Coke Machine
Suppose that a machine sells bottles of Coca-Cola (Coke) for 50 cents and only accepts coins in these denominations: 25 cents, 10 cents, and 5 cents.
In a file called coke.py
, implement a program that prompts the user to insert a coin, one at a time, each time informing the user of the amount due. Once the user has inputted at least 50 cents, output how many cents in change the user is owed. Assume that the user will only input integers, and ignore any integer that isn’t an accepted denomination.
Demo
Before You Begin
Log into cs50.dev, click on your terminal window, and execute cd
by itself. You should find that your terminal window’s prompt resembles the below:
$
Next execute
mkdir coke
to make a folder called coke
in your codespace.
Then execute
cd coke
to change directories into that folder. You should now see your terminal prompt as coke/ $
. You can now execute
code coke.py
to make a file called coke.py
where you’ll write your program.
How to Test
Here’s how to test your code manually:
- Run your program with
python coke.py
. Type25
and press Enter. Your program should output:Amount due: 25
and continue prompting the user for coins.
- Run your program with
python coke.py
. Type10
and press Enter. Your program should output:Amount due: 40
and continue prompting the user for coins.
- Run your program with
python coke.py
. Type5
and press Enter. Your program should output:Amount due: 45
and continue prompting the user for coins.
- Run your program with
python coke.py
. Type30
and press Enter. Your program should output:Amount due: 50
because the machine doesn’t accept 30-cent coins! Your program should then continue prompting the user for coins.
- Run your program with
python coke.py
. Type25
and press Enter, then type25
again and press Enter. Your program should halt and display:Change owed: 0
- Run your program with
python coke.py
. Type25
and press Enter, then type10
and press Enter. Type25
again and press Enter, after which your program should halt and display:Change owed: 10