CS21 Lab 3: Conditionals and Boolean Operators

Due Saturday, 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.

Vowel Replacement

Write a program vowels.py that replaces all occurrences of the letters a, e, i, o, or u by another symbol given by the user. Your program will take in two inputs:

  1. The string to modify

  2. The symbol to replace vowels with. This could be another letter, a symbol, or a digit.

Then, your program should output a new string, resulting from replacing all vowels with the above symbol. You can ignore uppercase vowels in your replacement.

$ python3 vowels.py
Enter some text: computer science is fun
Enter a symbol to replace vowels with: $

Your modified text is: c$mp$t$r sc$$nc$ $s f$n
$ python3 vowels.py
Enter some text: onomatopoeia
Enter a symbol to replace vowels with: x

Your modified text is: xnxmxtxpxxxx
$ python3 vowels.py
Enter some text: My favorite number is 7
Enter a symbol to replace vowels with: 7

Your modified text is: My f7v7r7t7 n7mb7r 7s 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. Ask the user to input some text (e.g., a word or a phrase) and a single character (a letter, symbol, or digit)

  2. Output the string with all vowels replaced by the given character, formatted as shown in the examples.

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

Cell Phone Family Plan Cost

Write a program that calculates the monthly price for a family cell phone plan. The cell phone plan is priced at a base rate of $60 for the first phone line. Each additional phone line added to the family plan is priced as follows:

Line Number Additional Price

Second Line

$50

Third Line

$45

Lines 4+

$40 for each phone line over three

So the price for two phone lines is $60 + $50 = $110; for three phone lines, it’s $60 + $50 + $45 = $155; each additional phone line past three costs an additional $40.

Additionally, there is a student discount offered, reducing the total monthly cost by 10%. Your program should ask the user to input the number of phone lines, and then ask the user if the student discount applies. The user should enter either 'y' (for 'yes', the student discount applies) or 'n' (for 'no', the student discount doesn’t apply). You may assume the user enters either 'y' or 'n'. Then, the program should compute and output the final monthly cost.

$ python3 phone.py
How many phone lines? 2
Is someone on the family plan a student? y

Your monthly cost is: $99
$ python3 phone.py
How many phone lines? 5
Is someone on the family plan a student? n

Your monthly cost is: $235
$ python3 phone.py
How many phone lines? -1
Number of phone lines can't be negative.

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 to enter a number, followed by answering y or n as to whether the student discount applies.

  2. The program should validate the first input to make sure it is positive (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. The program should compute and output the monthly cost, using the table above. The output should be formatted to match the examples 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 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!