CS21 Lab 4:

Due Saturday, February 19, by 11:59pm

Programming Tips

As you write programs, use good programming practices:

  • Use a comment at the top of the file to describe the purpose of the program (see example).

  • All programs should have a main() function (see example).

  • Use variable names that describe the contents of the variables.

  • Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.

  • Don’t assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them.

  • Avoid writing any lines of code that exceed 80 columns.

    • Always work in a terminal window that is 80 characters wide (resize it to be this wide)

    • In vim, at the bottom left in the window, there is an indication of both the line and the column of the cursor.

Are your files in the correct place?

Make sure all programs are saved to your cs21/labs/04 directory! Files outside that directory will not be graded.

$ update21
$ cd ~/cs21/labs/04
$ pwd
/home/username/cs21/labs/04
$ ls
Questions-04.txt
(should see your program files here)

Goals

The goals for this lab assignment are:

  • Learn to use Boolean logic and if-elif-else structures

  • Continue working with loops and accumulators

1. Egg sizing

Write a program called chickens.py that asks the user for the weight of a chicken egg in grams and the prints the corresponding size for that egg. The USDA sizes by weight are shown below.

size grade

Min weight (g)

Peewee

35

Small

43

Medium

50

Large

57

Extra Large

64

Jumbo

71

For example egg less than 35 grams is too small to grade, a 52g egg is classified as medium, a 64g egg is extra large, and anything 71g or larger is considered jumbo.

Some examples of running the program are shown below. User input is shown in bold.

$ python3 chickens.py
Welcome to the chicken egg sizing program
Enter the weight of the chicken egg in grams: 52

This is a medium egg.

$ python3 chickens.py
Welcome to the chicken egg sizing program
Enter the weight of the chicken egg in grams: 30

This is a too small to grade egg.

$ python3 chickens.py
Welcome to the chicken egg sizing program
Enter the weight of the chicken egg in grams: 64

This is a extra large egg.

$ python3 chickens.py
Welcome to the chicken egg sizing program
Enter the weight of the chicken egg in grams: 72.2

This is a jumbo egg.

Your program should have the following features:

  • You can assume the user enters a numeric value for the weight

  • Print an appropriate message for any weight, including eggs too small to grade.

2. Olympic Medal Count

With the 2022 Beijing Olympic Games happening this week, write a program called olympics.py that summarizes the number of medals won by a particular country. Prompt the user the enter a string of g, s, and b characters in any order (perhaps the order of winning events), to indicate gold, silver, and bronze medal wins, respectively. Your program should parse the string and print out a total count of gold, silver, and bronze medals won. If the total number of medals won is greater than zero, print a congratulatory message. Otherwise, provide a note of encouragement for Summer 2024 in Paris.

Some examples of running the program are shown below. User input is shown in bold.

$ python3 olympics.py
Let's summarize olympic medals
Enter medals as a string of g,s, and b, e.g., bggsb: gsg

Medal Summary:
  Gold 2
  Silver 1
  Bronze 0
Congratulations!

$ python3 olympics.py
Let's summarize olypmic medals
Enter medals as a string of g,s, and b, e.g., bggsb: ggssbbsgbgsgsgb

Medal Summary:
  Gold 6
  Silver 5
  Bronze 4
Congratulations!


$ python3 olympics.py
Let's summarize olypmic medals
Enter medals as a string of g,s, and b, e.g., bggsb:    no input here

Medal Summary:
  Gold 0
  Silver 0
  Bronze 0
Better luck in Summer 2024!

Your program should have the following features:

  • You can safely ignore any characters that aren’t g, s, or b.

  • Print out a count of 0 for any medal classification where no medals were won.

3. Modeling Spread (again)

Write a program called viral.py that estimates the revenue generated from a viral TikTok video. We’ll use a completely made up model for revenue.

  • A post with fewer than 10000 likes earns nothing.

  • For any post with 10,000 or more likes, you earn $3 per every 1000 likes. A video with 10,000 likes would earn $30. Payment is made in groups of 1000 likes, so a video with 10,999 likes still earns $30, but a video with 11,000 likes earns $33.

  • Any post with more than 1,000,000 likes earns an additional $2,500 from a sponsorship.

  • Any post with more than 10,000,000 likes earns an extra $10,000 in addition to the $2,500 sponsorship bonus.

Some examples of running the program are shown below. User input is shown in bold.

$ python3 viral.py

Enter number of likes: 9000
Your viral video with 9000 likes could earn $0

$ python3 viral.py

Enter number of likes: 10000
Your viral video with 10000 likes could earn $30

$ python3 viral.py

Enter number of likes: 10302
Your viral video with 10302 likes could earn $30

$ python3 viral.py

Enter number of likes: 50412
Your viral video with 50412 likes could earn $150

$ python3 viral.py

Enter number of likes: 2000919
Your viral video with 2000919 likes could earn $8500

$ python3 viral.py

Enter number of likes: 11000406
Your viral video with 11000406 likes could earn $45500

You can assume the user enters an integer value for the number of likes.

Answer the Questionnaire

Each lab will have a short questionnaire at the end. Please edit the Questions-04.txt file in your cs21/labs/04 directory and answer the questions in that file.

Once you’re done with that, you should run handin21 again.

Submitting lab assignments

Remember to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.

Logging out

When you’re done working in the lab, you should log out of the computer you’re using.

When Remotely logged in

When you are ssh’ed into the CS labs, first quit any applications you are running, like vim, then simply type exit at the prompt in your terminal window to disconnect.

When Physically logged in

When you are in a CS lab logged into a CS machine. First quit any applications you are running, like the browser and the terminal. Then click on the logout icon (logout icon or other logout icon) and choose "log out".

If you plan to leave the lab for just a few minutes, you do not need to log out. It is, however, a good idea to lock your machine while you are gone. You can lock your screen by clicking on the lock xlock icon. PLEASE do not leave a session locked for a long period of time. Power may go out, someone might reboot the machine, etc. You don’t want to lose any work!