CS21 Lab 3: Conditionals and Boolean Operators

Due Saturday, February 17, by 11:59pm

Goals

The goals for this lab assignment are:

  • Learn to use if/elif/else statements.

  • Learn to format the output of what is printed to the terminal.

  • Get more practice using the accumulator pattern.

  • Practice writing code to perform input validation.

Getting and Working on Lab03 Code

Make sure you are working in the correct directory when you edit and run lab 03 files.

Run update21 to get the starting point code of the next lab assignment. This will create a new ~/cs21/labs/03 directory for your lab work. Edit and run Lab 03 programs from within your cs21/labs/03 directory.

Make sure all programs are saved to your cs21/labs/03 directory! Files outside that directory will not be graded.

$ update21
$ cd ~/cs21/labs/03
$ pwd
/home/username/cs21/labs/03
$ ls
(should see your program files here)

Then edit and run program files in your ~/cs21/labs/03 directory:

$ code filename.py
$ python3 filename.py

1. Calculating Tuition Bill

In this part of the lab, you’ll write a program called tuitionbill.py that calculates a student’s tuition bill based on the number of credits they’re registering for, any overdue balance, and whether they are exempt from late penalties.

The rules for calculating the bill are as follows:

  • If the number of credits is 0, then the bill is equal to the overdue balance

  • If the number of credits is between 1 and 3, then the bill is $7000 per credit, plus the overdue balance

  • If the number of credits is 4 or 5, then the bill is $6000 per credit, plus the overdue balance

  • If the number of credits is 6 or more, then the bill is $5500 per credit, plus the overdue balance

  • Additionally, if the number of credits and the balance are both positive, and the student is not exempt from late penalties, then the total bill should be increased by 10%

1.1. Example Output

Here are a few runs of a working program (user inputs are in bold):

$ python3 tuitionbill.py

How many credits is the student registering for? 3
What is the current overdue balance? 225.50
Is the student exempt from late penalties? (y/n) y

The total bill is $21225.50


$ python3 tuitionbill.py

How many credits is the student registering for? 2
What is the current overdue balance? 1500.75
Is the student exempt from late penalties? (y/n) n

The total bill is $17050.83


$ python3 tuitionbill.py

How many credits is the student registering for? 0
What is the current overdue balance? 450

The total bill is $450.00


$ python3 tuitionbill.py

How many credits is the student registering for? 6
What is the current overdue balance? -225.0

The total bill is $32775.00

Note in the last two examples that the program does not ask the user if the student is exempt from late penalties because credits is 0 in the first example and the balance is negative in the second.

Note also that the total bill is displayed to two decimal places of precision.

1.2. Required Features

  • Your program should prompt the user to enter the number of credits and the overdue balance amount.

  • If the number of credits and the overdue balance are both positive, the program should also ask whether the student is exempt from late penalties.

  • The total tuition bill should then be displayed to two decimal places of precision using the rules described above.

1.3. Hints/Tips

  • Your program may assume that the user will enter a non-negative integer (i.e., 0 or greater) for credits, a floating point number for balance, and either "y" or "n" for exempt.

  • Be sure to test your program with different values for credits based on the ranges listed above, and also different combinations with positive and negative values for balance and with exempt being "y" or "n". In addition to the examples provided above, use a calculator to figure out what the output should be for each of the different cases that you try. You may want to create a spreadsheet to track the different test inputs and whether they are producing the right output.

  • Review this week’s class notes for information about how to display the total bill to only two decimal places of precision.

2. Rainfall

In this part of the lab, you’ll write a program called rainfall.py that calculates the maximum and average rainfall amounts of a location over a given period of time.

The program should ignore negative inputs for the rainfall amounts and only report results for amounts that are positive or zero.

2.1. Example Output

Here are a few runs of a working program (user inputs are in bold):

$ python3 rainfall.py

For how many months will you provide input? 3
How much rain in month #1? 5
How much rain in month #2? 2.2
How much rain in month #3? 7.6

The maximum amount is 7.60
The average of the 3 months is 4.93


$ python3 rainfall.py

For how many months will you provide input? 5
How much rain in month #1? 5.5
How much rain in month #2? 11.0
How much rain in month #3? 6.2
How much rain in month #4? 0
How much rain in month #5? 11

The maximum amount is 11.00
The average of the 5 months is 6.74

Note that the maximum amount and average are always displayed to two decimal places of precision.

If data is only reported for one month, then "1 month" (singular) should be shown when reporting the average, whereas the program should display "months" (plural) otherwise, as in the following example:

$ python3 rainfall.py

For how many months will you provide input? 1
How much rain in month #1? 3

The maximum amount is 3.00
The average of the 1 month is 3.00

2.2. Ignoring Invalid Inputs

In addition to the functionality as described above, your program should ignore any negative values for the rainfall amounts and only display the maximum and average of the values that are zero or positive (user inputs are in bold):

$ python3 rainfall.py

For how many months will you provide input? 4
How much rain in month #1? 5
How much rain in month #2? -2
How much rain in month #3? 7
How much rain in month #4? 4

The maximum amount is 7.00
The average of the 3 months is 5.33

In the case above, the -2 is ignored so the average is (5 + 7 + 4) / 3 = 5.33.

The program should display an error message if the user does not provide any non-negative inputs for the rainfall amounts (user inputs are in bold):

$ python3 rainfall.py

For how many months will you provide input? 4
How much rain in month #1? -1
How much rain in month #2? -2
How much rain in month #3? -3
How much rain in month #4? -4

No valid rainfall entries were provided.

2.3. Required Features

Your program should have the following features:

  • Prompt the user to enter the number of months for which they will provide input.

  • Then prompt the user to enter the rainfall amount for each month. Note that the prompt should include the month number, starting with 1. You can assume that the user will enter a numerical input (integer or floating point number).

  • Display the maximum and average of the non-negative rainfall amounts, i.e. those that are zero or positive; if only negative amounts are entered, show an error message instead.

  • The maximum value and average should be displayed to two decimal places of precision.

2.4. Hints/Tips

  • You may assume that the user will enter numerical (integer or floating point) values for the rainfall amounts, and that they will enter a positive integer for the number of months.

  • When showing the month number in the prompt, keep in mind that the string that is passed to the input() function can be dynamically generated using string concatenation or string formatting.

  • Review previous examples of the accumulator pattern and think about how you can apply them here.

  • Get the main functionality working first, then handle the special cases after everything else seems okay.

3. Parity Bit

In computer networking, data is sent between the Sender and Receiver using binary numbers, which are sequences of bits, or 0s and 1s, such as 001010 or 1101110101 and so on.

To detect any corruption that may occur during transmission, an extra bit is added to the end of the data so that the number of 1s is either even or odd, depending on the parity that the Sender and Receiver have agreed to use. If the number of 1s is not as expected, then a transmission error must have occurred. This extra bit of data is known as the parity bit.

For instance, if the Sender and Receiver are using even parity and the Sender wants to send 0011100, then a 1 is added to the end of the data so that the number of 1s is even; thus the data becomes 00111001, which now contains an even number of 1s.

However, if they are using odd parity, then a 0 would be added to the end of that data, which would become 00111000, since the number of 1s was already odd.

Write a Python program called parity.py that asks the user to enter the data as a sequence of 0s and 1s, and then asks what type of parity they are using ("even" or "odd"), and then displays the modified data with the appropriate parity bit appended at the end.

3.1. Example Output

Here are a few runs of a working program (user inputs are in bold):

$ python3 parity.py

Enter the data: 01010
What type of parity ('even' or 'odd')? even

The modified data is 010100


$ python3 parity.py

Enter the data: 01010
What type of parity ('even' or 'odd')? odd

The modified data is 010101


$ python3 parity.py

Enter the data: 01110
What type of parity ('even' or 'odd')? even

The modified data is 011101


$ python3 parity.py

Enter the data: 01110
What type of parity ('even' or 'odd')? odd

The modified data is 011100

3.2. Input Validation

In addition to the above functionality, your program should display appropriate error messages if the data entered by the user contains anything other than 0s and 1s, or if the user enters something other than 'even' or 'odd' for the type of parity (user inputs are in bold):

$ python3 parity.py

Enter the data: 00a11

Please enter a binary number consisting only of 0s and 1s.


$ python3 parity.py

Enter the data: 10101
What type of parity ('even' or 'odd')? platypus

Please enter 'even' or 'odd' as the type of parity.

3.3. Required Features

  • Your program should prompt the user to enter the data and the type of parity, and then display the data with the appropiate parity bit appended at the end.

  • If the user enters data consisting of anything other than 1s and 0s (including an empty string), or enters something other than "even" or "odd" as the parity, the program should show an appropriate error message.

3.4. Hints/Tips

  • Get the main functionality working first, then add the input validation after everything else seems okay.

  • Be sure to test your program with different combinations of even/odd numbers of 1s in the data and even/odd parity. You may want to create a spreadsheet to track the different test inputs and whether they are producing the right output.

  • You can use the Python exit() function to end your program once you have detected invalid input.

Programming Tips

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.

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!