CS21 Lab 3: If / Else
Due Saturday, September 28, before midnight
As you write your first programs, start using good programming practices now:
Use a comment at the top of the file to describe the purpose of the program.
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.
Write programs with
Format numerical string output.
Solve problems using
Continue using the accumulator pattern.
Apply incremental development, testing, and debugging strategies.
1. Blood Donation
Write a program
donation.py that matches a blood donor’s blood type to the
blood types of
|Donor Type||Acceptable Recipients|
A, B, AB, O
For example (user input is shown in bold):
$ python3 donation.py Enter donor's blood type: A This donor can donate to people with blood types: A, AB $ python3 donation.py Enter donor's blood type: AB This donor can donate to people with blood types: AB
If the user inputs an invalid donor blood type, you should let them know:
$ python3 donation.py Enter donor's blood type: OJ That is not a valid blood type. $ python3 donation.py Enter donor's blood type: Motor oil That is not a valid blood type.
For valid blood types, you may assume that the user enters upper-case letters. It’s fine to report an error for lowercase input:
$ python3 donation.py Enter donor's blood type: B This donor can donate to people with blood types: B, AB $ python3 donation.py Enter donor's blood type: b That is not a valid blood type.
2. Run-Length Encoding
A compression algorithm encodes data into a succinct format so that it’s cheaper to store or transmit. We’ll be using a variant of a real compression algorithm called run-length encoding (RLE).
For this problem, write a program called
decoder.py that decodes a
user-provided string to retrieve its human-readable form. For our version of
RLE, if a string has a number, it indicates that you should copy the next
letter that many additional times. For example (user input is shown in bold):
$ python3 decoder.py Enter encoded string: he2y heyyy $ python3 decoder.py Enter encoded string: y5et3 bo4i yeeeeeet boiiiii
Note that any character following a number should print one more time than
the number’s value (e.g., the
2y above prints
y three times).
You may assume that any number in an encoded string will be just a single digit (i.e., in the range [0-9]). You may also assume that no number will appear at the end of an input string.
Hint: As you iterate through the characters in the encoded string, you can
isdigit() function from the provided
lab3 library on a character
to determine whether it’s a number or a letter. The
True if the character is a number and
>>> isdigit('a') False >>> isdigit('3') True
To use the
isdigit() function, don’t forget to import it from the
from lab3 import isdigit
3. Bulk Discounts
Most manufacturers offer bulk pricing discounts. Write a program
to compute a buyer’s total cost, including bulk discounts, when purchasing one
or more products.
You should first ask the user how many products they plan to purchase. Then, for each product, ask for the product’s base price (prior to any discounts) and then ask how many of that product the user intends to buy. Apply the applicable bulk discount and print the discounted cost of that product. After the user has entered all the products, print the total price.
Apply discounts to products according to the following rules:
1 - 10
11 - 50
51 - 100
When computing a bulk discount, multiply the price by
1 - discount
percentage. For example, if the user is buying 70 copies of an item that
costs $100, you can apply the bulk discount of 7% and compute the total using:
70 * 100 * (1 - 0.07) = 6510
You may assume that the strings the user inputs can be converted to the appropriate variable type to answer the question. That is, for questions asking "how many products", you’ll be given a string that can be converted to an int, and for questions asking about prices, you’ll be given a string that can be converted to a float.
Here are two example runs (user input is shown in bold):
$ python3 pricing.py How many products would you like to buy? 2 What is product 1's base price? 20 How many of product 1 will you be buying? 30 Product 1's cost: $576.00 What is product 2's base price? 100.00 How many of product 2 will you be buying? 60 Product 2's cost: $5580.00 Total cost: $6156.00
$ python3 pricing.py How many products would you like to buy? 3 What is product 1's base price? 3.50 How many of product 1 will you be buying? 20 Product 1's cost: $67.20 What is product 2's base price? 9.99 How many of product 2 will you be buying? 200 Product 2's cost: $1798.20 What is product 3's base price? 100 How many of product 3 will you be buying? 3 Product 3's cost: $300.00 Total cost: $2165.40
Hint: This program will be larger than the other two. We strongly suggest that you test your program in small pieces as you write it rather than waiting until the end to test everything. You might want to start by asking "How many products would you like to buy?" and using the user’s response to execute a simple loop that prints that number of times. After confirming that part works via testing, you can proceed to ask the remaining questions.
4. Answer the Questionnaire
Each lab will have a short questionnaire at the end. Please edit
Questions-03.txt file in your
and answer the questions in that file.
Once you’re done with that, run
Turning in your labs….
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
you complete each program or after you complete significant
work on any one program.