Lab 11: Defining Functions¶

In this lab, you are going to practice defining your own functions, including those with parameters and return values.

Warm Up¶

We could write a function to calculate a tip at a restaurant like the following, which mutiplies the check amount by 15% and then displays the result. Try out this code and run some of your own tests as well.

def tip_calculator(amount):
    tip = amount * 0.15
    print(tip)
    
#let's perform some unit tests on this function
tip_calculator(12.87)
tip_calculator(110.20)
tip_calculator(37.95)
1.9304999999999999
16.53
5.6925

One improvement we could make is to use the built-in round() function so that we can display dollar amounts with only two decimal places:

def tip_calculator(amount):
    tip = round(amount * 0.15,2)
    print(tip)
    
#let's perform some unit tests on this function
tip_calculator(12.87)
tip_calculator(110.20)
tip_calculator(37.95)
1.93
16.53
5.69

You'll now work on a few more improvements to this function.

Excercise 1: Change the tip_calculator so that it takes two arguments instead of one. The second argument should be the percentage tip that you want to calculate. You should be able to call it like this:

tip_calculator(12.87,18) #prints 2.32
tip_calculator(110.20,15) #prints 16.53
tip_calculator(37.95,20) #prints 7.59
2.32
16.53
7.59

Exercise 2: As we discussed, since the purpose of the above function is to compute a value, it's a bad idea to just print it inside the function. Instead, the value should be returned. Change the tip_calculator function so that it returns the value instead. You should now be able to use it with custom code like this:

cost = float(input("What was the total cost of food? "))
print('With 18% gratuity, your total bill is',cost+tip_calculator(cost,18))
What was the total cost of food? 43.50
With 18% gratuity, your total bill is 51.33

In your notes, write why the return statement allows this to work but the first version (with the print) did not.

In the future, it will almost always be better to return the value from a function rather than print it - it allows you a lot more flexibility in how the function can be used.

Exercise 3: Rewrite your letter grade exercise from the Lab 6: Chained Conditional and Logical Operator Practice lab so that the function takes a numerical grade as an argument and then returns the letter grade. You should be able to call it and use the result inside a print statement like this (you can do this in either interactive or script mode, but this shows the interactive version):

Exercise 4: In the Lab 8: Loops and Lists lab, you wrote some code that would convert a list of readings in inches to readings in centimeters. Create a new Python file called list_convert.py and write this as a function called list_in_to_cm() where the inches list is taken as an argument and the centimeters list is returned. Recall that to convert an inches measurement to centimeters, you multiply it by 2.54. When you unit-test your function, it should look like this:

Hint: Unlike Exercise 3, you probably won't be able to just put your Lab 8 code into a function definition. Instead, you will need to write a new loop inside you function definition which uses values from the parameter (which is the inches list) and builds a new list with the equivalent amounts in centimeters.

When you are finished, submit your solution to the Lab 11: List conversion function assignment on codePost. One of the other nice things about functions is that codePost can perform unit tests on them too, so you don't have to write the user input/output statements and make sure they match the words my tests are looking for! On the other hand, you must submit a .py file with the exact name list_convert.py and your function has to have the exact name list_in_to_cm(). If either of those things is a different name, it won't be able to find and run your code.