F19 ECOR1051 Lab5
F19 ECOR1051 Lab5
Objectives
Learning outcomes: 4, 5; Graduate attributes: 1.3, 5.3 (see the course outline)
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.
Remember, you don’t have to finish the exercises during your scheduled Lab 5 session on
Monday, Tuesday or Wednesday; however, you should complete any unfinished exercises on
your own time before Lab 6 (Wednesday, Thursday or Friday).You have until Sunday, Sept. 29
at 11:59 pm to submit your solutions for grading.
Exercise 1
Step 1: Create a new editor window and save it as a file named lab5ex1_3.py. (Your solutions
to Exercises 1, 2 and 3 will be saved in this file.)
Step 2: Here is the definition of function area_of_disk, which returns the area of a circle with a
non-negative radius. Type the code in the editor window:
import math
def area_of_disk(radius):
return math.pi * radius ** 2
Save the code, then click Run. Correct any syntax errors.
Step 3: Add appropriate type annotations to the function header. Add a documentation string to
the function definition.(For information about type annotations and docstrings, review Designing
New Functions: A Recipe in Practical Programming, 3rd ed., pp. 47-58). Save the code, then
click Run. Correct any syntax errors.
1
Exercise 2
Use the function design recipe to design, code and test the definition of a function named
distance. This function returns the distance between two points, given by the coordinates
(x1 , y1 ) and (x2 , y2). The function parameters are the x and y values. (For those students who know
Python, don't use lists or tuples to represent the points.)
http://mathworld.wolfram.com/PythagoreanTheorem.html
Exercise 3
Use the function design recipe to design, code and test the definition of a function named
area_of_circle. This function takes two points: the center of a circle, (xc, yc), and a point on
the perimeter, (xp , yp ),
and returns the area of the circle. The function parameters are the x and y
values.
Exercise 4
Step 1: Create a new editor window and save it as a file named lab5ex4.py.
Step 2: You've volunteered to repaint the walls in Leo's Lounge and will need to use a ladder
when painting close to the ceiling. You know that a ladder has to be placed against a wall at an
angle; otherwise, it will fall over. Use the function design recipe to design, code and test the
definition of a function named height. This function takes the length of a ladder, measured in
metres, and the angle that it forms with the ground as it leans against the wall, measured in
degrees. It returns the height reached by the ladder.
● must have type annotations and a complete docstring. Think carefully about the range of
permitted values for the two parameters. Remember to document these in your docstring.
● must have exactly one statement in the function body: return followed by an
expression. Do not use local variables.
2
Hint 1: the most challenging part of this exercise is the math, not the Python code. In Step 1 of
the FDR, use pre-university trigonometry to express the solution's formula using mathematical
notation before you prepare the function call examples. In Step 4, translating the formula to
Python should be straightforward.
Hint 2: when you're coding the function body, use the shell to investigate which trigonometric
functions are provided by Python's math module:
Wrap Up
1. Login to cuLearn and click the link Submit Lab 5 for grading.
3. Submit your solutions to by dragging the files lab5ex1_3.py and lab5ex4.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.
4. 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.)
5. To finish submitting your lab work, click the Submit assignment button.
6. 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. After copying your function definitions into the
PyTutor editor, you will have to type assignment statements that call the functions and assign the
returned values to variables.