CS21 Lab 3: Conditionals and Boolean Operators

Due Saturday, September 28, 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 have a top-level comment! Please see our function example page if you are confused about writing function comments.

Gen Z Translator

This is meant to be a fun / silly translation program to practice if statements. Real translation is much more sophisticated than simple word replacement.

Every generation has its own slang. Let’s write a program to help older folks, like your CS instructors, communicate in your "language".

Your program should prompt the user for a number of words, and for each word, check to see if there’s a Gen-Z substitute. Words that match the list below should be substituted, and any other word should be kept as typed. At the end, print both the original and "translated" sentences.

The word substitutions are:

Word(s) Substitution

embarrassing or awkward

cringe

style or clothes

drip

family

fam

good or great

fire

honestly

legit

charisma

rizz

suspicious

sus

gossip

tea

Sample output

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

$ python3 genz.py
How many words will you enter? 6
Enter word 1: Narples
Enter word 2: food
Enter word 3: is
Enter word 4: honestly
Enter word 5: pretty
Enter word 6: good

The original sentence is:
Narples food is honestly pretty good.

The translated sentence is:
Narples food is legit pretty fire.
$ python3 genz.py
How many words will you enter? 4
Enter word 1: Dad's
Enter word 2: style
Enter word 3: is
Enter word 4: embarrassing

The original sentence is:
Dad's style is embarrassing.

The translated sentence is:
Dad's drip is cringe.
$ python3 genz.py
How many words will you enter? 8
Enter word 1: That
Enter word 2: gossip
Enter word 3: about
Enter word 4: your
Enter word 5: family
Enter word 6: was
Enter word 7: honestly
Enter word 8: suspicious

The original sentence is:
That gossip about your family was honestly suspicious.

The translated sentence is:
That tea about your fam was legit sus.

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 for the number of words and, for each word, prompt the user to enter the word with an increasing number (e.g., "word 1", "word 2", etc.).

  2. Print both the original and translated text matching the formatting shown in the examples above. Substitutions should only appear in the "translated" version.

  3. The words in your sentences should have one space between them, and your sentences should end in a period (with no spaces before the first word and no spaces between the final word and the period).

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

  • You may assume that the user enters a positive integer for the number of words to enter.

  • Rather that performing substitutions on the full sentence after the user has entered every word, you probably want to accumulate two sentences as you go along, word-by-word.

Computing Grades

This semester, CS 21 will be graded based on a combination of standards and lab completion. Let’s write a program to simulate how this grading system will work to compute a student’s grade.

Your program should prompt the user twice:

  1. Asking for a 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. 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).

To simplify the number of if statements, 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 a B.
$ python3 grades.py
How many standards did the student achieve? 15
Average lab completion percentage? 72.1

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

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

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

This student earned a 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.

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 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!