Problem 3
Trish at Bargain Used Books continues to use the programs you developed for her. You expect that at some point, Trish will ask you to extend the types of items that she currently sells. So you've decided to update the program you did for Lab 03 to use functions.
You will be rewriting Lab03P3.py to use functions. Your updated program will not change what the program does, but it will make it easier to enhance.
Create a file named
Lab04P4.py. Write a program that calculates and displays all the values Trish needs when completing a customer purchase:
· Define a function called
get_item_cost
that takes three parameters: the name of an item type, the maximum number of that item which can be purchased, and the unit price. The function will prompt the user for the number of that particular item that are being purchased, validate the number entered is between 0 and the max allowed number, and then return the total cost for that specific item. Here's what a function call to that function looks like:
pb_total = get_item_cost('paperback books', 50, 2.50)
· Define a function called
calculate_tax_amount
that takes two parameters: The pre-tax amount and the tax rate. The function calculates the tax and returns it. Here's what a function call to that function looks like:
tax = calculate_tax_amount(total_before_tax, 0.07)
· Write a
main
function that calls the methods above to do the following:
o Ask the user to enter the number of paperback books being purchased. The maximum allowed is 50 and the unit cost of the paperbacks is $2.50.
o Ask the user to enter the number of hardback books being purchased. The maximum allowed is 20 and the unit cost of the hardbacks is $7.00.
o Ask the user to enter the number of magazines being purchased. The maximum allowed is 35 and the unit cost of the magazines is $3.95.
o Calculate the total before tax.
o Calculate the sales tax using the function you created. Use the amount 0.07 (7%) for the sales tax.
o Calculate the total after tax.
o Print a receipt that contains the following information:
§ Total cost of paperbacks (if not zero, otherwise skip this step)
§ Total cost of hardbacks (if not zero, otherwise skip this step)
§ Total cost of magazines (if not zero, otherwise skip this step)
§ A divider line
§ Subtotal before tax
§ Amount of tax to be charged
§ Total including tax
Here's some sample outputs of running the program:
Enter the number of paperback books: 5
Enter the number of hardback books: 3
Enter the number of magazines: 2
Paperbacks: $12.50
Hardbacks: $21.00
Magazines: $7.90
---------------------
Subtotal: 41.40
Tax: 2.90
Amount due: 44.30
Enter the number of paperback books: -2
Number of paperback books must be between 0 and 50
Enter the number of paperback books: 4
Enter the number of hardback books: 0
Enter the number of magazines: 7
Paperbacks: $10.00
Magazines: $27.65
---------------------
Subtotal: 37.65
Tax: 2.64
Amount due: 40.29
NOTE: We are still
not
handling issues where a user types an invalid integer, that is, an entry which cannot be converted using the int() function. We will handle that in a future lab.
Run this program using the PyCharm Terminal. Take a screenshot of the Terminal that includes the line showing where you started the program run with the results. Name the screenshot
Lab04P3-ouput.jpg.
Submit both files,
Lab04P3.py
and
Lab04P3-output.jpg, to Blackboard for credit.
Bonus Opportunity – 10 Points
· Copy Lab04P3.py to another file and name it Lab04Bonus.py.
· Add two new item types to be sold:
Item type
|
Max Allowed
|
Unit Price
|
Bargain books
|
10
|
$1.00
|
Bookmarks
|
100
|
$0.35
|
· Define a new function called
calculate_discount
that takes one parameter: The cost before tax. If the cost before tax is more than $50, calculate a 10% discount. If the cost before tax is more than $100, calculate a 20% discount. Return the discount amount.
· Add logic to include a discount. Discounts are applied BEFORE tax. Tax is applied AFTER the discount is subtracted from the before tax amount.
· Include a line to the receipt showing the amount of discount received. The line should go between the before tax amount and the tax line:
Enter the number of paperback books: 5
Enter the number of hardback books: 9
Enter the number of magazines: 4
Enter the number of bargain books: 3
Enter the number of bookmarks: 12
Paperbacks: $12.50
Hardbacks: $63.00
Magazines: $15.80
Bargain books: $3.00
Bookmarks: $4.20
---------------------
Subtotal: 98.50
Discount: 9.85
Tax: 6.21
Amount due: 94.86