I’m working on an ATM algorithm assignment where the user inputs the amount of money they want to deposit. The code should then pop out the appropriate number bills required like hundreds, fifties, twenties, tens, and ones.
The input is only to be between 1 and 200. We’re not to use advanced features since this is an intro class (and we’ll get points taken off) so what we can use are if/else statements, booleans, arithmetic operators, and some others that are not currently related to this assignment.
So, if I input 19 or 55 into this algorithm, it pops out at 1 ten or 1 fifty without the remaining 9 or 5.
money = int(input("Enter amount between 1 and 200: "))
hundreds = money // 100
fifties = money // 50
twenties = money // 20
tens = money // 10
fives = money // 5
ones = money // 1
if (money > 200 or money < 1):
print("Invalid amount. Enter a number between 1 and 200.")
else:
print("You have deposited", money, "dollar bill(s).")
if (money >= 100):
print("Hundreds:", hundreds)
elif (money >= 50):
print("Fifties:", fifties)
elif (money >= 20):
print("Twenties:", twenties)
elif (money >= 10):
print("Tens:", tens)
elif (money >= 5):
print("Fives:", fives)
elif (money >= 1):
print("Ones:", ones)
Output:
Enter amount between 1 and 200: 19
You have deposited 19 dollar bill(s).
Tens: 1
Enter amount between 1 and 200: 55
You have deposited 19 dollar bill(s).
Fifties: 1
>Solution :
money = int(input("Enter amount between 1 and 200: "))
if money > 200 or money < 1:
print("Invalid amount. Enter a number between 1 and 200.")
print("You have deposited", money, "dollar bill(s).")
hundreds = money // 100
money = money % 100
fifties = money // 50
money = money % 50
twenties = money // 20
money = money % 20
tens = money // 10
money = money % 10
fives = money // 5
money = money % 5
ones = money // 1
print("Hundreds:", hundreds)
print("Fifties:", fifties)
print("Twenties:", twenties)
print("Tens:", tens)
print("Fives:", fives)
print("Ones:", ones)
Look up the Modulo operator in Python (%). Hope I was helpful.
What this code does is divides the money sequentially, first by 100, then 50 etc as you were already doing. In between those steps, however, we can get the modulo of the money which is the remainder of the division by the note value (what’s left over after we divide it). We can then divide the remainder of the division by each note value, and do this until there is no more remainder.