Cash
Problem to Solve
In a file called cash.py
in a folder called sentimental-cash
, write a program that asks the user how much change is owed and then spits out the minimum number of coins with which said change can be made. You can do this exactly as you did in Problem Set 1, except that your program this time should be written in Python, and you should assume that the user will input their change in dollars (e.g., 0.50 dollars instead of 50 cents).
Demo
Specification
- Use
get_float
from the CS50 Library to get the user’s input andprint
to output your answer. Assume that the only coins available are quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢).- We ask that you use
get_float
so that you can handle dollars and cents, albeit sans dollar sign. In other words, if some customer is owed $9.75 (as in the case where a newspaper costs 25¢ but the customer pays with a $10 bill), assume that your program’s input will be9.75
and not$9.75
or975
. However, if some customer is owed $9 exactly, assume that your program’s input will be9.00
or just9
but, again, not$9
or900
. Of course, by nature of floating-point values, your program will likely work with inputs like9.0
and9.000
as well; you need not worry about checking whether the user’s input is “formatted” like money should be.
- We ask that you use
- If the user fails to provide a non-negative value, your program should re-prompt the user for a valid amount again and again until the user complies.
- Incidentally, so that we can automate some tests of your code, we ask that your program’s last line of output be only the minimum number of coins possible: an integer followed by a newline.
How to Test
While check50
is available for this problem, you’re encouraged to first test your code on your own for each of the following.
- Run your program as
python cash.py
, and wait for a prompt for input. Type in0.41
and press enter. Your program should output4
. - Run your program as
python cash.py
, and wait for a prompt for input. Type in0.01
and press enter. Your program should output1
. - Run your program as
python cash.py
, and wait for a prompt for input. Type in0.15
and press enter. Your program should output2
. - Run your program as
python cash.py
, and wait for a prompt for input. Type in1.60
and press enter. Your program should output7
. - Run your program as
python cash.py
, and wait for a prompt for input. Type in23
and press enter. Your program should output92
. - Run your program as
python cash.py
, and wait for a prompt for input. Type in4.2
and press enter. Your program should output18
. - Run your program as
python cash.py
, and wait for a prompt for input. Type in-1
and press enter. Your program should reject this input as invalid, as by re-prompting the user to type in another number. - Run your program as
python cash.py
, and wait for a prompt for input. Type infoo
and press enter. Your program should reject this input as invalid, as by re-prompting the user to type in another number. - Run your program as
python cash.py
, and wait for a prompt for input. Do not type anything, and press enter. Your program should reject this input as invalid, as by re-prompting the user to type in another number.
Correctness
check50 cs50/problems/2024/x/sentimental/cash
Style
style50 cash.py
How to Submit
submit50 cs50/problems/2024/x/sentimental/cash
Why does my submission pass check50, but shows “No results” in my Gradebook after running submit50?
In some cases, submit50
may not grade the assignment due to inconsistent formatting in your cash.py
file. To fix this issue, run black cash.py
in the sentimental-cash
folder. Address any issues that are revealed. Run check50
again to ensure your submission still functions. Finally, run the submit50
command above again. Your result will appear in your Gradebook within a few minutes.
Please note that if there is a numerical score next to your cash submission in the submissions
area of your Gradebook, the procedure discussed above does not apply to you. Likely, you have not fully addressed the requirements of the problem set and should rely upon check50
for clues as to what work remains.