Cash
Implement a program that calculates the minimum number of coins required to give a user change.
$ python cash.py
Change owed: 0.41
4
Specification
Instructions for Harvard College students
- Head to GitHub and, after signing in, accept this assignment on GitHub Classroom.
- After about a minute, refresh the page and click the link to visit your personal GitHub Classroom assignment page.
- On the assignment page, click the green Code button and choose Open with Codespaces.
- Cilck New codespace and then, if prompted, Create codespace.
- Once your Codespace loads, click the
+
button in the bottom section of your window (next to the word âbashâ). You should then see blue text appear that says/workspaces/sentimental-cash-USERNAME
(whereUSERNAME
is your GitHub username).
Instructions for non-Harvard College students
Create a new directory called cash
inside of your pset6
directory by executing
~/ $ mkdir ~/pset6/cash
Create a new file called cash.py
inside your cash
directory.
- In
cash.py
, write a program that first asks the user how much change is owed and then spits out the minimum number of coins with which said change can be made, exactly as you did in Problem Set 1, except that your program this time should be written in Python. - 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.
Usage
Your program should behave per the example below.
$ python cash.py
Change owed: 0.41
4
Testing
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.
Execute the below to evaluate the correctness of your code using check50
. But be sure to compile and test it yourself as well!
check50 cs50/problems/2021/summer/sentimental/cash
Execute the below to evaluate the style of your code using style50
.
style50 cash.py
This problem will be graded only along the axes of correctness and style.
How to Submit
Instructions for Harvard College students
Harvard College students (those with an @college.harvard.edu email address) should submit this problem via GitHub, not via Gradescope.
In your Codespace, execute the below, replacing USERNAME
with your actual GitHub username.
submit50 classroom50/sentimental-cash USERNAME
Instructions for non-Harvard College students
- Download your
cash.py
file by control-clicking or right-clicking on the file in CS50 IDEâs file browser and choosing Download. - Go to CS50âs Gradescope page.
- Click âProblem Set 6: Sentimental (Cash)â.
- Drag and drop your
cash.py
file to the area that says âDrag & Dropâ. Be sure it has the correct filename, or the autograder will fail to run on it, and it will score no correctness points! - Click âUploadâ.
You should see a message that says âProblem Set 6: Sentimental (Cash) submitted successfully!â You wonât see a score just yet, but if you see the message then weâve received your submission!