CS21 Lab 3: Conditionals and Boolean Operators

Due Monday February 23, before 11:59pm

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.

Numbers As Letters

Some numbers look similar to letters. We will write a program numberletter.py which will take a string given by the user and replace some of the letters with their numerical lookalikes.

Specifically, write a program that does the following:

  1. Asks the user to input some text

  2. Creates a new string that replaces:

    1. Each lowercase o and capital O with the number 0

    2. Each lowercase s and capital S with the number 5

    3. Each lowercase l with the number 1

  3. Prints the new string.

Below are some example outputs.

$ python3 numberletter.py
Enter some text: i like computer science
Your modified text is: i 1ike c0mputer 5cience
$ python3 numberletter.py
Enter some text: I LOVE Computer Science
Your modified text is: I L0VE C0mputer 5cience
$ python3 numberletter.py
Enter some text: CS21 Labs are fun!
Your modified text is: C521 Lab5 are fun!

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)

  2. Output the text with all lowercase l, lowercase or capital o/O, and lowercase or capital s / S, letters replaced by 1, 0, and 5, respectively, as described above.

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

Car Rental Cost Calculator

When renting a car, the price is $50 per day when the rental is for five days or less. Rentals exceeding five days are charged a weekly rate of $300 per week; any number of days over a week are charged as a full week. For example, a rental of 6 or 7 days is considered "one week", costing $300, while 8—​14 days would be considered "two weeks", costing $600.

Additionally, the rental permits driving a maximum of 100 miles for each day of the rental. Any additional miles driven over this allowance are charged at a rate of $0.10 per mile.

Write a program that asks the user to enter the number of days for a car rental and the number of miles driven, and calculates the total cost of the rental. You should verify that the number of days for the rental and the number of miles driven are both positive integers; if the user enters 0 or a negative number for either, print out a message and exit the program. To exit, you can simply call the exit() function.

At the end, your program should display the total cost.

Hint: In calculating the number of weeks, it may be helpful to use the mod operator % and the "floor division" operator //. If a rental is for days days, days // 7 gives the number of full weeks while days % 7 gives the remaining number of days left over. For example, if days is 10, days // 7 will be 1 and days % 7 will be 3 (since 10 days is one week plus 3 days).

Below are some example outputs.

$ python3 carrental.py
Enter the number of days for the car rental: 5
Enter the number of miles driven: 256

The cost for this car rental is 250
$ python3 carrental.py
Enter the number of days for the car rental: 2
Enter the number of miles driven: 256

The cost for this car rental is 105.6
$ python3 carrental.py
Enter the number of days for the car rental: 6
Enter the number of miles driven: 400

The cost for this car rental is 300
$ python3 carrental.py
Enter the number of days for the car rental: 9
Enter the number of miles driven: 900

The cost for this car rental is 600
$ python3 carrental.py
Enter the number of days for the car rental: 14
Enter the number of miles driven: 1000

The cost for this car rental is 600
$ python3 carrental.py
Enter the number of days for the car rental: 15
Enter the number of miles driven: 1000

The cost for this car rental is 900
$ python3 carrental.py
Enter the number of days for the car rental: 20
Enter the number of miles driven: 2001

The cost for this car rental is 900.1
$ python3 carrental.py
Enter the number of days for the car rental: 4
Enter the number of miles driven: -10
Number of miles must be positive
$ python3 carrental.py
Enter the number of days for the car rental: 0
Number of days must be positive

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 two numbers, the number of days for the rental and the number of miles driven.

  2. The program should validate the both inputs to make sure they are positive (you may assume the user enters an integer). If the user doesn’t enter a valid number for the days or miles, the program should output an error and exit. To exit the program, you can call the exit() function.

  3. The program should compute and output the total cost for the rental, as described 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.

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.

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!