COS 100: Introduction to Programming
Interim 2021
HW Project 06: Handling money
Due: 01/19 Tue 10pm
Give me neither poverty nor riches—
Feed me with the food allotted to me;
Lest I be full and deny You,
And say, "Who is the Lord?"
Or lest I be poor and steal,
And profane the name of my God.
Feed me with the food allotted to me;
Lest I be full and deny You,
And say, "Who is the Lord?"
Or lest I be poor and steal,
And profane the name of my God.
Proverbs 30:8b–9
Project goal
Create a simple Point of Sale system.Prerequisites
- Finish lab07 first: it is designed to prepare you for this project.
Project specification
Follow the steps below to write a program that can handle simple tasks for a cashier.-
Write a function called
ask_for_currency
with the following specification:- Parameter: a string to be used as a prompt.
- Ask the user for a number using the prompt passed to this function.
- This number is a currency amount (such as
$34.59
) but without the dollar sign in front (such as34.59
). - Convert this to an integer number of pennies (such as
3459
). - Return: the integer number of pennies.
-
Write a function called
format_currency
with the following specification:- Parameter: a currency amount represented as an integer number of pennies.
- Convert this number (such as
3459
) to a string in the standard way we write currency (such as$34.59
), with a dollar sign in front and two decimal places. - Return: the string representing the amount.
-
Write (and call) a main function that asks the user for:
- the number of items to purchase,
- the price for each item,
- the amount tendered,
- the total purchase price,
- the amount tendered, and
- the change that's due.
Notes and hints
-
Name your project source code
change.py
. - You must use functions properly. If your main function does not use the two other functions you wrote, you will lose points.
- You may assume that the numbers the user enter are positive.
- If the amount tendered is less than the total purchase price, do something reasonable.
-
The user should enter currency amounts in dollars without the dollar sign (e.g.,
34.6
or.45
), but your output should format with a dollar sign and two decimal places (e.g.,$5.40
). - Try to always display two decimal places. This should be achieved by techniques you learned involving arithmetic. Do not use string slicing.
Suggested order of development
- As always, try to code this up a little bit at a time, testing after each feature you've added.
- Specifically, write one function at a time and test that function thoroughly.
-
For example, to test the
format_currency
function, you can write, at the end of your file, things likeprint(format_currency(3459))
to see if the function is working. -
If you see
None
printed to the screen, it may mean thatformat_currency
is not returning any value. - Get this fully working before you even start to write a main function.
- For the main function, start by just asking for the price of one item, amount tendered, and the report.
- Then finally add the ability to purchase multiple items.
Sample run
Number of items? 2 Price? 20 Price? 14.59 Amount tendered? 40 Report ====== Purchase total: $34.59 Amount tendered: $40.00 Change due: $5.41
Grading
- Total points: 20
ask_for_currency
function: 5- Make sure the parameter is a string and the return value is an integer.
format_currency
function: 5- Make sure the parameter is an integer and the return value is a string.
main
function: 6- Style: 4
- Follow all the previous style guidelines, and also:
- Remember to document the purpose of each function with a triple-quoted docstring (see lab for more explanations).
Optional challenge
- Also report the change in dollars, quarters, dimes, nickels, and pennies. Your answer should be the natural one—the one that gives away as little small change as possible.
-
Sample run:
Number of items? 2 Price? 20 Price? 14.59 Amount tendered? 40 Report ====== Purchase total: $34.59 Amount tendered: $40.00 Change due: $5.41 5 one-dollar bills 1 quarter 1 dime 1 nickel 1 penny
-
If you want, you may submit this extension to Moodle as
detailed.py
, but it will not be graded. - This assignment is inspired in part by one created by Jeff Ondich.
Start early, have fun, and discuss questions on Moodle.