CSE 111: Programming with Functions

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.

storage_efficiency =
volume
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:

volume = π radius2height
surface_area = 2π radius (radius + height)

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

  1. Your program must compute the volume of all 12 can sizes.
  2. Your program must compute the surface area of all 12 can sizes.
  3. 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.

  1. Add another function named compute_storage_efficiency to your program. This function should call the compute_volume and compute_surface_area functions and then compute and return the storage efficiency of a steel can size. Replace code in the main function with a call to the compute_storage_efficiency function as appropriate. Did adding and calling the compute_storage_efficiency function reduce the number of lines of code in your program?
  2. 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 the compute_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?”
  3. 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.
  4. 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

  1. Create a folder for this week's code-along program.
  2. Open the folder you just created in VSCode.
  3. Create a new file named can_sizes.py.
  4. 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
Testing Procedure

Verify that your program works correctly by following each step in this testing procedure:

  1. 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:

  1. The computer starts executing the sample program by calling the main function.
  2. While executing the main function, the computer calls the compute_volume, compute_surface_area, and print functions.
A function call graph for this assignment

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:

  1. The computer starts executing the stretch program by calling the main function.
  2. While executing the main function, the computer calls the compute_storage_efficiency function which calls the compute_surface_area and compute_volume functions.
  3. While still executing the main function, the computer calls the compute_cost_efficiency function which calls the compute_volume function.
  4. Finally, while still executing the main function the computer calls the print function.
A function call graph for the stretch challenges of this assignment

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:

Useful Links: