Professional Documents
Culture Documents
Objectives
● To gain experience developing Python functions and programs that perform different
computations, depending on whether or not a condition is fulfilled.
Learning outcomes: 2, 4, 5; Graduate attributes: 1.3, 5.3 (see the course outline)
Due Date
You don’t have to finish the exercises during your scheduled Lab 7 session on Monday, Tuesday
or Wednesday; however, you should complete any unfinished exercises on your own time before
Lab 8 (Wednesday, Thursday or Friday).You have until Sunday, Oct. 6 at 11:59 pm to submit
your solutions for grading.
Getting Started
Launch Wing 101. When Python starts, it prints a message in the shell window. The first line
should contain “3.7.4”, which indicates that Wing is running Python version 3.7. If another
version is running, ask a TA for help to reconfigure Wing to run Python 3.7.
Step 1: Create a new editor window and save it as a file named lab7ex1.py.
Step 2: Calculating the tip when you go to a restaurant is not difficult, but a restaurant wants to
suggest a tip based on the diners' satisfaction with the level of service they receive. Use the
function design recipe from Chapter 3 of Practical Programming to develop a function named
tip. The function's first argument is the cost of the meal. The second argument is satisfaction
level. (Use these ratings: 1 = Totally satisfied, 2 = Somewhat satisfied, 3 = Dissatisfied). The
function returns the amount of the tip, calculated as follows:
Your function definition must have type annotations and a complete docstring. Use the Python
shell to test your function.
1
Exercise 2 (Based on an exercise by Cay Horstmann and Rance Necaise)
Step 1: Create a new editor window and save it as a file named lab7ex2.py.
Step 2: A supermarket awards coupons depending on how much a customer spends on groceries.
For example, if you spend $50, you'll get a coupon worth 8% of that amount ($4). The following
table shows the percentage used to calculate the coupon awarded for different amounts spent:
Use the function design recipe to develop a function named coupon. The function's argument is
the amount spent on groceries. It returns the value of the coupon.
Your function definition must have type annotations and a complete docstring. Use the Python
shell to test your function.
Hint: you need to code the conditions that determine the coupon percentage. If you think
carefully about the order in which the conditions will be executed, you'll see that you don't need
to use the Boolean operators (and, or and not) in the conditions.
2
Exercise 3
Step 1: Create a new editor window and save it as a file named lab7ex3.py.
Step 2: The factorial n! of a positive integer n is defined as:
Here is an incorrect definition of a function that calculates the factorial of its argument. Don't
worry if you don't understand the function body: it uses Python constructs that have not yet been
covered in class, but that's not a problem, because you won't be correcting the function in this
exercise.
>>> factorial(1)
1
>>> factorial(2)
2
>>> factorial(3)
6
>>> factorial(4)
24
"""
fact = 1
for i in range(2, n + 1):
fact = fact * n
return fact
Type the function definition in the editor window. Use the Python shell to test factorial,
using the four examples shown in the docstring. Which tests pass? Which tests fail?
Step 3: Calling functions from the shell is a convenient way to do initial confidence testing, but
it's not the best tool for performing rigorous testing. You have to determine manually whether or
not the tests pass by comparing the results displayed by Python to the values you expect the
function to return. If you have numerous test cases, it's easy to skip one when you're typing the
call expressions, one at a time. To improve testing, why not write a program that tests the
function we're developing?
In lab7ex3.py, after the definition of factorial, write a program that calls factorial four
times, with arguments 1, 2, 3 and 4, respectively. For each call, it will determine whether the
actual result (that is, the value returned by the implementation of factorial that you typed in
Step 2) matches the expected result (the value a correct implementation of factorial should
3
return). The output produced by the program should look like this:
Testing factorial(1)
Expected result: 1 Actual result: 1
Test passed
Testing factorial(2)
Expected result: 2 Actual result: 2
Test passed
Testing factorial(3)
Expected result: 6 Actual result: 9
Test failed
Testing factorial(4)
Expected result: 24 Actual result: 64
Test failed
2 tests passed
2 tests failed
Important: don't hardwire the actual result values (1, 2, 9 and 64) into your program.
Instead, it will display the values returned by the four calls to factorial. Also, your program
has to determine if each test passed or failed, and keep track of the number of passing and failing
tests.
Hint: By default, print prints its arguments separated by one space; for example,
If one of the arguments is sep = "a_string", the values will be separated by a_string. In
this example, three periods are printed between the values:
If sep is assigned the empty string, '', no spaces are printed between the values:
4
Wrap Up
Login to cuLearn and click the link Submit Lab 7 for grading.
2. Submit your solutions to by dragging the files lab7ex1.py, lab7ex2.py and lab7ex3.py
to the File submissions box. Click the Save changes button. The Submissions
status page will be displayed. The line labelled Submission status will indicate Draft (not
submitted), which means that you can make changes. The line labelled File submissions
will list the names of the files you've submitted. Verify that the files you've submitted
have the correct names. File with different names will not be marked.
3. If you want to make changes to your submission, click the Edit submission button. You
can then delete or replace any of the files. After you've edited your submission, click the
Save changes button. The Submissions status page will be displayed. (If you log out
of cuLearn after submitting a draft of your solutions, the submission status will still be
Draft (not submitted) the next time you login before the submission due date.)
4. To finish submitting your lab work, click the Submit assignment button.
5. When the Confirm submission page appears, click the Continue button to submit your
work. The status of your submission will change to Submitted for grading. If you've
changed your mind, click the Cancel button. This will return you to the Submission
status page. Completing the submission process is important. You cannot change
the submission status after the due date. Only submissions with status Submitted for
grading will be graded.
Extra Practice
During the midterm and final exams, you will be expected to draw diagrams similar to those
created by Python Tutor. Use PyTutor to visualize the execution of your exercise solutions.
Remember, PyTutor doesn't have a shell. For Exercises 1 and 2, after copying your function
definitions into the PyTutor editor, you will have to type assignment statements that call the
function and assign the returned values to variables.