#TODO (Advanced): Introduce the notion of adding default values
def meal_cost(food_cost, tax_rate=8.875, tip_perc=15):
total_cost = food_cost + food_cost * (tax_rate/100) + food_cost * (tip_perc/100)
total_cost = round(total_cost,2)
return total_cost
food = 50
cost = meal_cost(food)
print(f"The cost of the meal will be: ${cost}")
food = 50
tax = 8.875
tip = 15
cost = meal_cost(food, tax, tip)
print(f"The cost of the meal will be: ${cost}")
food = 50
tax = 8.875
cost = meal_cost(food, tax)
print(f"The cost of the meal will be: ${cost}")
food = 50
tip = 20
# THIS DOES NOT WORK. COMPARE WITH THE CODE BELOW
# IF WE HAVE TWO PARAMETERS THE SECOND ONE IS ASSUMED TO BE TAX, NOT TIP
cost = meal_cost(food, tip)
print(f"The cost of the meal will be: ${cost}")
name=value
convention for the parameter¶food = 50
tax = 8.875
tip = 20
cost = meal_cost(food, tax, tip)
print(f"The cost of the meal will be: ${cost}")
#TODO: Introduce the notion of calling by naming the parameters
cost = meal_cost(food_cost=30, tax_rate=8.875, tip_perc=15)
print(f"The cost of the meal will be: ${cost}")
We will now write a non-math oriented function. We want a function that takes as input a piece of text, and returns the number of words in it. What is the length of the article in words? For simplicity we assume that words are separated by spaces.
article = """One person was believed to be missing after an oil rig storage platform exploded Sunday night on Lake Pontchartrain, just north and west of New Orleans. Seven people were taken to hospitals after the explosion, and three of them remained in critical condition Monday morning, Mike Guillot, the director of emergency medical services at East Jefferson General Hospital, said at a news conference. The other four were released. Sheriff Joe Lopinto of Jefferson Parish said at the news conference, We are fairly confident there is an eighth person, adding that search efforts were continuing, and the Coast Guard had contacted the family of the person. No fatalities had been reported as of Monday morning. The blast occurred shortly after seven pm near St Charles Parish and the city of Kenner. The platform is in unincorporated Jefferson Parish. Officials are still investigating the cause of the explosion, but the City of Kenner said on its Facebook page that authorities on the scene report that cleaning chemicals ignited on the surface of the oil rig platform."""
print(article)
words = article.split(" ")
print(words)
len_words = len(words)
print(len_words)
So, in this case, we have the input being the string variable that contains the text, and the output is a number.
def article_length(text):
words = text.split(" ")
length = len(words)
return length
len_words = article_length(article)
print(f"The article length is {len_words} words")
# TODO: Return also the length in characters, and the length in sentences
# Introduce the notion of multiple values in the output
Here is another example of a function, again math oriented, so that you remember your high school years.
We want to solve the quadratic equation $ a*x^2 + b*x + c = 0$.
Recall that the solutions of a quadratic equation $a \cdot x^2+ b \cdot x+c$ are:
import math
a = 1
b = -5.86
c = 8.5408
# Start solving
D = b**2 - 4*a*c
if D>0:
x1 = (-b + math.sqrt(D) ) /(2*a)
print(f"Solution 1: {x1:.3f}")
x2 = (-b - math.sqrt(D) )/(2*a)
print(f"Solution 2: {x2:.3f}")
elif D==0:
x = -b/(2*a)
print(f"Solution: {x:.3f}")
else:
print(f"No Solution")
So, let's convert the code above into a function. Let's start by taking the code above and putting it in a function.
# NOTE: This is an INCORRECT APPROACH:
# Using PRINT instead of RETURN
def solve_quadratic(a, b, c):
D = b**2 - 4 * a * c
if D > 0:
x1 = (-b + math.sqrt(D) ) / (2 * a)
print(f"Solution 1: {x1:.3f}")
x2 = (-b - math.sqrt(D) ) / (2 * a)
print(f"Solution 2: {x2:.3f}")
elif D == 0:
x = -b / (2 * a)
print(f"Solution: {x:.3f}")
else:
print(f"No Solution")
print
instead of return
¶The code above is a very common mistake for beginners. It is a tricky mistake because the program superficially seems to work fine.
a = 1
b = -5.86
c = 8.5408
solve_quadratic(a, b, c)
a = 1
b = 2
c = 1
solve_quadratic(a, b, c)
Notice though one issue: There is no output.
a = 1
b = 2
c = 1
result = solve_quadratic(a, b, c)
print("The result is:", result)
So, what is the input and output in this case?
For input, things are clear: We need the values for $a$, $b$, and $c$.
For output? We may need to get back two, one, or no values. This is an example where we need to think before writing the code, and make a design decision.
A common solution when we have situations with multiple possible outputs is to return a tuple, as we did above.
## COMMON MISTAKE 1: PRINT instead of RETURN
# Example of returning "multivalued" results using tuples/lists
def solve_quadratic(a,b,c):
# We can even check that the value of the discriminant
# is positive before returning a result
discr = b**2 - 4*a*c
if discr < 0: # We will not compute
return None # "None" is a special value, meaning "nothing"
if discr == 0:
solution = -b / (2 * a)
return solution
if discr > 0:
solution1 = (-b + math.sqrt(D) ) / (2 * a)
solution2 = (-b - math.sqrt(D) ) / (2 * a)
# A function can return a list, tuple, dictionary, etc.
# The "return" value does not have to be a single value
return solution1, solution2
solutions = solve_quadratic(a,b,c)
print("Solutions:", solutions )
## COMMON MISTAKE 2
# Using multiple return statements
# Why? After we execute the first return,
# we do not execute anything below that
def s1(a, b, c):
d = math.sqrt(b**2 - 4*a*c)
return (-b + d)/(2*a) # solution 1
return (-b - d)/(2*a) # solution 2, BUT this will never be executed
# this function takes as input a phone (string variable)
# and prints only its digits
def clean(phone):
result = ""
digits = {"0","1","2","3","4","5","6","7","8","9"}
for c in phone:
if c in digits:
result = result + c
return result
p = "(800) 555-1214 Panos Phone number"
print(clean(p))
# this function takes as input a phone (string variable)
# and prints only its digits
def clean(phone):
# We initialize the result variable to be empty.
# We will append to this variable the digit characters
result = ""
# This is a set of digits (as **strings**) that will
# allow us to filter the characters
digits = {"0","1","2","3","4","5","6","7","8","9"}
# We iterate over all the characters in the string "phone"
# which is a parameter of the function clean
for c in phone:
# We check if the character c is a digit
if c in digits:
# if it is, we append it to the result
result = result + c
# once we are done we return a string variable with the result
return result
# This is an alternative, one-line solution that uses a list
# comprehension to create the list of acceptable characters,
# and then uses the join command to concatenate all the
# characters in the list into a string. Notice that we use
# the empty string "" as the connector
def clean_oneline(phone):
digits = {"0","1","2","3","4","5","6","7","8","9"}
return "".join([c for c in phone if c in digits])
### Example: Random password
n
letters. The value n
should be a parameter.¶# This code generates one random letter
import random
import string
random.choice(string.ascii_letters)