0% found this document useful (0 votes)
69 views6 pages

F19 ECOR1051 Lab4

This document provides instructions for students to complete a lab assignment involving defining and testing functions in Python. The lab contains 4 exercises where students write functions to calculate the area of geometric shapes, convert between fuel efficiency units, calculate interest accumulation, and calculate the surface area of a cone. Students are instructed to save their code files, test the functions interactively, and submit specific code files for grading through the university's online learning system.

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)
69 views6 pages

F19 ECOR1051 Lab4

This document provides instructions for students to complete a lab assignment involving defining and testing functions in Python. The lab contains 4 exercises where students write functions to calculate the area of geometric shapes, convert between fuel efficiency units, calculate interest accumulation, and calculate the surface area of a cone. Students are instructed to save their code files, test the functions interactively, and submit specific code files for grading through the university's online learning system.

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/ 6

Carleton University

Department of Systems and Computer Engineering


ECOR 1051 - Fundamentals of Engineering I - Fall 2019

Lab 4 - Defining and Testing Functions

Objectives

To gain experience using Wing 101 to edit function definitions and interactively test functions.

Learning outcomes: 4, 5; Graduate attributes: 1.3, 5.3 (see the course outline)

Getting Started

Python Tutor, which you used in the last lab, is a great tool for visualizing the execution of
programs, but it is not a complete program development environment and it doesn't provide a
way for us to save our code in a file. If we're writing more than a few lines of code, we typically
prepare it using an editor and save it in a ​source code file​.

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.

For this lab, you ​do not​ have to write ​documentation strings​ for your functions (b​rief descriptions
of what the functions do and examples of how to use the shell to test the functions). ​The body of
each function must​ ​have exactly one statement: ​return​ followed by an expression.​ Your
functions ​may not​ use local variables.

Remember, you don’t have to finish the exercises during the lab period. You have until Sunday,
Sept. 22 at 11:59 pm to complete any unfinished exercises on your own time and submit your
solutions for grading.

1
Exercise 1

Step 1​: Click Wing's ​Create a new file​ button (the leftmost button in the toolbar - it looks like a
piece of paper) or select ​File > New​ from the menu bar. A new editor window, labelled
untitled-1.py​, will appear.

Step 2:​ From the menu bar, select ​File > Save As...​ A "​Save Document As​" dialogue box
will appear. Navigate to the folder where you want to store the file, then type ​lab4ex1.py​ in the
File name:​ box. Click the ​Save​ button. Wing will create a new file named ​lab4ex1.py​.

Step 3:​ In the editor window, type this code (which you used in Lab 3):

import math

def area_of_disk(radius):
return math.pi * radius ** 2

def area_of_ring(outer, inner):


return area_of_disk(outer) - area_of_disk(inner)

After you've edited the code, save it in ​lab4ex1.py​. To do this, click the ​Save​ button in the
toolbar or select ​File > Save​ from the menu bar.

Step 4:​ Click the ​Run​ button (the green triangle in the toolbar). This will load ​lab4ex1.py​ into
the Python interpreter and check it for syntax errors. If any syntax errors are reported, edit the
function to correct the errors, save the edited file, then click ​Run​.

Step 5:​ Interactively test the functions by typing these ​call expressions​ (​function calls​) in the
shell. Python will display a number after each call. How do you interpret these numbers; that is,
what do they represent?

>>> area_of_disk(5)

>>> area_of_disk(5.0)

>>> area_of_ring(10, 5)

>>> area_of_ring(10.0, 5.0)

Step 6:​ Select ​File > Close​ from the menu bar. The editor window will no longer be displayed.
(You can click the ​Open a file​ button on the toolbar - it looks like an open file folder - or select
File > Open...​ from the menu bar if you want to reopen the file.)

2
Exercise 2

In Lab 2, you wrote Python code that converts 32 miles per Imperial gallon to (approximately)
8.83 litres per 100 km. In this exercise, you'll reimplement this code as a function that will
convert any fuel efficiency measured in mpg to the equivalent metric fuel consumption.

Step 1:​ Create a new editor window and save it as a file named ​lab4ex2.py​.

Step 2:​ Type these two assignment statements and the function header. (One Imperial gallon is
equal to approximately 4.54609 litres and one mile is equal to approximately 1.60934 km. Recall
that the names of constant values in Python are, by convention, usually written entirely in
uppercase.)

LITRES_PER_GALLON = 4.54609
KMS_PER_MILE = 1.60934

def convert_to_litres_per_100_km(mpg):

Complete the function definition.

Step 3:​ Save the code, then click ​Run​. Correct any syntax errors.

Step 4:​ Use the shell to test your function. What value does the function return when the
argument is 32? Is it correct? What value does the function return when the value is 0? Is it
correct? Does your function work for integer arguments and real number arguments?

Step 5:​ Close the editor window for ​lab4ex2.py​.

3
Exercise 3

In Lab 2, you wrote Python code that calculates the amount of money you'll have when money is
deposited in a bank account that earns interest. In this exercise, you'll reimplement this code as a
function.

Suppose you have some money (the ​principal)​ that is deposited in a bank account for a number
of years and earns a fixed annual ​rate​ of interest. The interest is compounded ​n​ times per year.

The formula for determining the amount of money you'll have is:

rate n·time
amount = principal(1 + n )
where:

● amount​ is the amount of money accumulated after ​time​ years, including interest.
● principal​ is the initial amount of money deposited in the account
● rate​ is the annual rate of interest, specified as a decimal; e.g, 5% is specified as 0.05
● n​ is the number of times the interest is compounded per year
● time​ is the number of years for which the principal is deposited.

For example, if $1,500.000 is deposited in an account paying an annual interest rate of 4.3%,
compounded quarterly (4 times a year), the amount in the account after 6 years is:
0.043 4·6
amount = $1, 500(1 + 4 ) ≈ $1938.84

Step 1:​ Create a new editor window and save it as a file named ​lab4ex3.py​.

Step 2:​ Type this function header:

def accumulated_amount(principal, rate, n, time):

Complete the function definition.

Step 3:​ Save the code, then click ​Run​. Correct any syntax errors.

Step 4:​ Use the shell to test your function. Verify that your function returns the correct value for
the example shown earlier, you should check other cases. For example, what value would you
expect the function to return if the principal is $0? If the interest rate is 0%? Does your function
return correct values for these test cases?

Step 5:​ Close the editor window for ​lab4ex3.py​.

4
Exercise 4

The lateral surface area of a right-circular cone (a right cone with a base that is a circle) can be
calculated by the function:

f : h, r → f (h, r); f (h, r) = π r × √r 2 + h2

where ​h​ is the height of the cone and ​r​ is the radius of the circular base.

Step 1:​ Use a calculator to determine the area of a right circular cone for various values of ​r​ and
h​. These will be your test cases for Step 5.

Step 2:​ Create a new editor window and save it as a file named ​lab4ex4.py​.

Step 3:​ Type this function header:

def area_of_cone(height, radius):

This function will return the lateral surface area of a right circular cone with the specified
non-negative height and radius. Complete the function definition. For the value of π, use variable
pi​ from the ​math​ module. Python's ​math​ module also has a function that calculates square
roots. To find out about this function, use Python's help facility. In the shell, type:

>>> import math

>>> help(math.sqrt)

Step 4:​ Save the code, then click ​Run​. Correct any syntax errors.

Step 5:​ Use the shell to test your function. Use the test cases you prepared in Step 1. For each
test, is the actual result (the value returned by the function) equal to the expected result?

Step 6:​ Close the editor window for ​lab4ex4.py​.

5
Wrap Up

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

2. Click the ​Add submission​ button.

3. Submit your solutions to Exercises 2, 3 and 4 by dragging the files ​lab4ex2.py​,


lab4ex3.py​ and ​lab4ex4.py​ ​to the ​File submissions​ box. ​Do not submit your file from
Exercise 1 (​lab4ex1.py​).​ ​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.

5. When you are ready 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. 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 solutions to Exercises 2,
3 and 4. 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. 14, 2019

You might also like