0% found this document useful (0 votes)
57 views3 pages

F19 ECOR1051 Lab5

This document provides instructions for students to complete Lab 5 exercises in the Fundamentals of Engineering I course at Carleton University. The exercises involve designing and testing functions using the function design recipe. Students are instructed to create functions for calculating the area of a disk, distance between two points, area of a circle, and the height of a ladder leaning against a wall. The functions must include type annotations and docstrings. Students are then directed to submit their function code files for grading through the course learning management system before the due date.

Uploaded by

Eric Brad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views3 pages

F19 ECOR1051 Lab5

This document provides instructions for students to complete Lab 5 exercises in the Fundamentals of Engineering I course at Carleton University. The exercises involve designing and testing functions using the function design recipe. Students are instructed to create functions for calculating the area of a disk, distance between two points, area of a circle, and the height of a ladder leaning against a wall. The functions must include type annotations and docstrings. Students are then directed to submit their function code files for grading through the course learning management system before the due date.

Uploaded by

Eric Brad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Carleton University

Department of Systems and Computer Engineering


ECOR 1051 - Fundamentals of Engineering I - Fall 2019

Lab 5 - Defining and Testing Functions

Objectives

To gain experience using ​Practical Programming​'s function design recipe (FDR).

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.

Step 4:​ Use the Python shell to test your function.

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​​ , ​y​2​). 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.)

If you don't remember the formula to use, check this page:

http://mathworld.wolfram.com/PythagoreanTheorem.html

Your function definition must have:

● type annotations and a complete docstring.


● exactly one​ statement in the function body: ​return​ followed by an expression.​ Do not
use local variables.

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, (​x​c,​ ​y​c), ​ and a point on
the perimeter, (​xp​ ​, ​yp​ ),
​ and returns the area of the circle. The function parameters are the ​x​ and ​y
values.

Your function definition:

● must have type annotations and a complete docstring.


● must have ​exactly one​ statement in the function body: ​return​ followed by an
expression​. Do not use local variables.
● must call the functions you wrote for Exercises 1 and 2.

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.

Your function definition:

● 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:

>>> import math


>>> help(math)

Wrap Up

1. Login to cuLearn and click the link ​Submit Lab 5 for grading​.

2. Click the ​Add submission​ button.

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.

Last edited: Sept. 20, 2019

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy