CS21 Lab 3: Conditionals and Boolean Operators

Due Sunday, September 27, before midnight

Goals

The goals for this lab assignment are:

  • Solve programming problems with if / elif / else statements.

  • Print formatted output to the terminal.

  • Validate user inputs to a program.

  • Practice using the accumulator pattern.

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 vscode, at the bottom right in the window, there is an indication of both the line and the column of the cursor.

Function Comments

All functions should include a comment explaining their purpose, parameters, return value, and describe any side effects. Please see our function example page if you are confused about writing function comments.

Text Changing Virus

There is a type of virus where text is altered. A version of this can make text really annoying to read. Sometimes called joke programs, these can add things like typos, gibberish, make all text have alternating capital letters (lIkE tHiS), etc. Create a program ‘joke.py’ that makes text just a little more annoying to read. It will take in one input string from the user and then create a new string, where all lowercase vowels (a, e, i, o, u) are an asterisk (*) and all spaces are underscores (_). It will then print the new string.

Remember: Strings are immutable.

Note: we do not condone the creation of actual viruses.

Sample output

Below are some examples of the running program. User input is shown in bold.

$ python3 joke.py

Enter string: Swarthmore College
New string: Sw*rthm*r*_C*ll*g*
$ python3 joke.py

Enter string: I love my dog
New string: I_l*v*_my_d*g
$ python3 joke.py

Enter string: SPIDERMAN
New string: SPIDERMAN
$ python3 joke.py

Enter string: NATIONAL HISTORICAL PARK
New string: NATIONAL_HISTORICAL_PARK

Requirements

The code you submit for labs is expected to follow good style practices, and to meet one of the course standards, you’ll need to demonstrate good style on six or more of the lab assignments across the semester. To meet the good style expectations, you should:

  • Write a comment that describes the program as a whole using a comment at the top of the file.

  • All programs should have a main() function.

  • Use descriptive variable names.

  • Write code with readable line length, avoiding writing any lines of code that exceed 80 columns.

  • (Later labs) Add comments to explain complex code segments.

  • (Later labs) Write a comment for each function (except main) that describes its parameters, return value, and purpose.

Your program should meet the following requirements:

  1. Ask the user to enter a string.

  2. Create a more difficult to read string where lowercase vowels are asterisks and spaces are underscores.

  3. Output the new string.

Your code should be contained in a main function that is called by your program.

Computing Grades

CS21 uses standards-based grading, and grades are based on a combination of standards and lab completion. For this part of the lab, we’ll write a program to compute a student’s grade.

Your program should do the following:

  1. Prompt the user, asking for the number of achieved standards. You may assume that user will enter an integer, but you’ll need to validate that the integer is in a sensible range (between 0 and 19).

  2. Prompt the user, asking for a lab completion percentage. You may assume that the user will enter a floating point number, but you’ll need to validate that the float is in a sensible range (0.0 to 100.0).

  3. Determine the appropriate letter grade for the number of standards and lab completion percentage achieved by the student, and print this out.

To simplify things a bit, we’ll use only the whole-letter grades:

Table 1. CS 21 Grade Requirements for Standards and Labs
Grade Standards Achieved Average Lab Completion

A

18

90%

B

16

80%

C

15

70%

D

12

60%

NC

Anything Below D

A student must meet both criteria in a row to earn the corresponding grade. For example, earning an A requires meeting 18 or more standards and a lab completion average of 90% or higher.

Sample output

$ python3 grades.py
How many standards did the student achieve? 16
Average lab completion percentage? 92.7

This student earned: B.
$ python3 grades.py
How many standards did the student achieve? 15
Average lab completion percentage? 72.1

This student earned: C.
$ python3 grades.py
How many standards did the student achieve? 19
Average lab completion percentage? 90

This student earned: A.
$ python3 grades.py
How many standards did the student achieve? 13
Average lab completion percentage? 75

This student earned: D.
$ python3 grades.py
How many standards did the student achieve? 10
Average lab completion percentage? 85

This student earned: NC.
$ python3 grades.py
How many standards did the student achieve? 20

20 is not a valid number of standards.
$ python3 grades.py
How many standards did the student achieve? -5

-5 is not a valid number of standards.
$ python3 grades.py
How many standards did the student achieve? 18
Average lab completion percentage? 110.5

110.50 is not a valid lab completion percentage.

Requirements

The code you submit for labs is expected to follow good style practices, and to meet one of the course standards, you’ll need to demonstrate good style on six or more of the lab assignments across the semester. To meet the good style expectations, you should:

  • Write a comment that describes the program as a whole using a comment at the top of the file.

  • All programs should have a main() function.

  • Use descriptive variable names.

  • Write code with readable line length, avoiding writing any lines of code that exceed 80 columns.

  • (Later labs) Add comments to explain complex code segments.

  • (Later labs) Write a comment for each function (except main) that describes its parameters, return value, and purpose.

Your program should meet the following requirements:

  1. If given valid input for both the number of standards and the lab completion percentage, it should determine and print the student’s grade according to the table above.

  2. If given invalid input for either prompt, print an error message matching the examples above and exit the program. To exit, you can simply call the exit() function.

Your output should match the examples shown above when given the same inputs. Your solution should be contained within a main function that you call at the end of your program.

Notes

  • When printing an invalid lab completion percentage, you can use the %.2f format code to print exactly two decimal places.

  • If you find yourself writing (or copying/pasting) an excessive number of nested if statements, you should consider an alternative (less tedious) approach to structuring your conditional checks.

Meal Plan Tracker

A school offers a meal plan where students can purchase meals throughout the week. Each meal costs $5. If a student purchases at least 5 meals in a week, they get a 10% discount. If a student purchases at least 10 meals, they get a 20% discount. Write a program meal.py that asks the user how many days they went to the dining hall and how many meals they purchased each day. At the end of the program, print the total number of meals purchased and the total amount spent on meals.

Sample output

Below are some examples of the running program. User input is shown in bold.

$ python3 meal.py
How many days did you go to the dining hall this week? 2
How many meals did you purchase on day 1? 2
How many meals did you purchase on day 2? 1
Total meals purchased: 3
Total spent on meals: $15
$ python3 meal.py
How many days did you go to the dining hall this week? 3
How many meals did you purchase on day 1? 2
How many meals did you purchase on day 2? 3
How many meals did you purchase on day 3? 1
Total meals purchased: 6
Total spent on meals: $27
$ python3 meal.py

How many days did you go to the dining hall this week? 5
How many meals did you purchase on day 1? 3
How many meals did you purchase on day 2? 3
How many meals did you purchase on day 3? 1
How many meals did you purchase on day 4? 3
How many meals did you purchase on day 5? 3

Total meals purchased: 13
Total spent on meals: $52
$ python3 meal.py

How many days did you go to the dining hall this week? -4
Please try again with a positive number less than or equal to 7.
$ python3 meal.py

How many days did you go to the dining hall this week? 100
Please try again with a positive number less than or equal to 7.

Requirements

The code you submit for labs is expected to follow good style practices, and to meet one of the course standards, you’ll need to demonstrate good style on six or more of the lab assignments across the semester. To meet the good style expectations, you should:

  • Write a comment that describes the program as a whole using a comment at the top of the file.

  • All programs should have a main() function.

  • Use descriptive variable names.

  • Write code with readable line length, avoiding writing any lines of code that exceed 80 columns.

  • (Later labs) Add comments to explain complex code segments.

  • (Later labs) Write a comment for each function (except main) that describes its parameters, return value, and purpose.

Your program should meet the following requirements:

  1. The program should prompt the user for how many days they went to the dining hall, followed by how many meals they ate each day.

  2. The program should validate the first input to make sure it is positive and less than or equal to 7 (you may assume the user enters an integer). If the user doesn’t enter a valid number of lines, the program should output an error and exit.

  3. Calculate how much the student owes, applying appropriate discounts.

  4. Output the total numbers of meals a student purchased and the total cost using the format above.

Your output should match the examples shown above when given the same inputs. Your solution should be contained within a main function that you call at the end of your program.

Answer the Questionnaire

After each lab, please complete the short Google Forms questionnaire. Please select the right lab number (Lab 03) from the dropdown menu on the first question.

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.

First quit any applications you are running, including your vscode editor, the browser and the terminal. Then click on the logout icon (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!