W02 Code-along Activity: Can Efficiency
Code-along Activity Overview
For this activity, you will code along with an instructor video that walks you through each step of the activity below. Make sure that you pause the video, or watch sections over if you need to, so that you are able to complete the entire activity.
Your instructor will post a link to the video in Microsoft Teams.
Background
In many countries, food is stored in steel cans (also known as tin cans) that are shaped like cylinders. There are many different sizes of steel cans. The storage efficiency of a can tells us how much a can stores versus how much steel is required to make the can. Some sizes of cans require a lot of steel to store a small amount of food. Other sizes of cans require less steel and store more food. A can size with a large storage efficiency is considered more friendly to the environment than a can size with a small storage efficiency.
The storage efficiency of a steel can is computed by dividing the volume of a can by its surface area.
In other words, the storage efficiency of a can is the space inside the can divided by the amount of steel required to make the can. The formulas for the volume and surface area of a cylinder are:
-
π is the constant PI, the ratio of the circumference of a circle divided by its diameter (use
math.pi
) -
radius is the radius of the cylinder
-
height is the height of the cylinder
Program
Work along with your instructor to write a Python program that computes and prints the storage efficiency for each of the following 12 steel can sizes. Then visually examine the output and answer this question, “Which can size has the highest storage efficiency?”
Name | Radius (centimeters) |
Height (centimeters) |
Cost per Can (U.S. dollars) |
---|---|---|---|
#1 Picnic | 6.83 | 10.16 | $0.28 |
#1 Tall | 7.78 | 11.91 | $0.43 |
#2 | 8.73 | 11.59 | $0.45 |
#2.5 | 10.32 | 11.91 | $0.61 |
#3 Cylinder | 10.79 | 17.78 | $0.86 |
#5 | 13.02 | 14.29 | $0.83 |
#6Z | 5.40 | 8.89 | $0.22 |
#8Z short | 6.83 | 7.62 | $0.26 |
#10 | 15.72 | 17.78 | $1.53 |
#211 | 6.83 | 12.38 | $0.34 |
#300 | 7.62 | 11.27 | $0.38 |
#303 | 8.10 | 11.11 | $0.42 |
Requirements
-
Your program must compute the volume of all 12 can sizes.
-
Your program must compute the surface area of all 12 can sizes.
-
Your program must compute and print the storage efficiency of all 12 can sizes.
Enhancements
Here is a list of enhancements that you could make to the program. Your instructor will walk you through at least one of them. Feel free to complete others.
-
Add another function named
compute_storage_efficiency
to your program. This function should call thecompute_volume
andcompute_surface_area
functions and then compute and return the storage efficiency of a steel can size. Replace code in themain
function with a call to thecompute_storage_efficiency
function as appropriate. Did adding and calling thecompute_storage_efficiency
function reduce the number of lines of code in your program? -
The table of can sizes that appears in the Assignment section above includes a column that contains the cost per can of each steel can size. Add another function to your program named
compute_cost_efficiency
that computes and returns the volume of a steel can divided by its cost. Write code to call thecompute_cost_efficiency
function and print the cost efficiency for each can size. Then visually examine the output and answer this question, “Which can size has the highest cost efficiency?” -
If you remember how to use lists and a for loop from CSE 110, rewrite your
main
function so that it uses a list or lists that contain the can size names and dimensions. Then write a loop that processes the values in the list. -
Add
if
statements inside the loop to automatically determine which can size has the best storage efficiency and which can size has the best cost efficiency.
Instructions
- Create a folder for this week's code-along program.
- Open the folder you just created in VSCode.
- Create a new file named can_sizes.py.
- Find the video that your instructor has posted for this assignment and code along with them to complete each of the requirements. Work through the requirements in order with the instructor rather than jumping ahead to more complicated steps to ensure you do not miss any concepts.
Alternative Video Link and Transcript
In addition to the link that your instructor posted, you are also welcome to watch the following code walkthrough or use its transcript:
Additional Resources
You may use these additional resources for further study.
Sample Solution
In addition to the instructor video, you can also view this solution. (This solution may be slightly different than the final code that you and your instructor created.)
Helpful Documentation
-
The preparation content explains how to write functions.
-
The preparation content local variable scope.
-
The Python
math
module contains mathematical constants and functions includingmath.pi
.
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
-
Run your program and verify that it prints the correct results, rounded and formatted as shown below.
> python can_sizes.py #1 Picnic 2.04 #1 Tall 2.35 #2 2.49 #2.5 2.76 #3 Cylinder 3.36 #5 3.41 #6Z 1.68 #8Z short 1.80 #10 4.17 #211 2.20 #300 2.27 #303 2.34
Call Graphs
The following call graph shows the function calls and returns in the sample solution for this assignment. From this call graph we see the following function calls:
-
The computer starts executing the sample program by calling the
main
function. -
While executing the
main
function, the computer calls thecompute_volume
,compute_surface_area
, andprint
functions.
The next call graph shows the function calls and returns in the sample solution for the stretch challenges of this assignment. From this call graph we see the following function calls:
-
The computer starts executing the stretch program by calling the
main
function. -
While executing the
main
function, the computer calls thecompute_storage_efficiency
function which calls thecompute_surface_area
andcompute_volume
functions. -
While still executing the
main
function, the computer calls thecompute_cost_efficiency
function which calls thecompute_volume
function. -
Finally, while still executing the
main
function the computer calls theprint
function.
Submission
Please pause and re-watch any sections of the instructor video necessary until you have completed the entire activity and your program runs the same as the instructor's. When you are finished:
- Return to Canvas to take the quiz.
Useful Links:
- Return to: Week Overview | Course Home