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/elsestatements. -
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:
-
Asks the user to input some text
-
Creates a new string that replaces:
-
Each lowercase
oand capitalOwith the number0 -
Each lowercase
sand capitalSwith the number5 -
Each lowercase
lwith the number1
-
-
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:
|
Your program should meet the following requirements:
-
Ask the user to input some text (e.g., a word or a phrase)
-
Output the text with all lowercase
l, lowercase or capitalo/O, and lowercase or capitals/S, letters replaced by1,0, and5, 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:
|
Your program should meet the following requirements:
-
The program should prompt the user to enter two numbers, the number of days for the rental and the number of miles driven.
-
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. -
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:
-
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).
-
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).
-
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:
| 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:
|
Your program should meet the following requirements:
-
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.
-
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
%.2fformat code to print exactly two decimal places. -
If you find yourself writing (or copying/pasting) an excessive number of nested
ifstatements, 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
(
or
) 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
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!