Why is my change calculator only working sometimes for some numbers

It only works with certain numbers and I’m not sure why. for example the number 69 works but the number 42 doesn’t

#Variables
quarter = 25
dime = 10
nickel = 5
penny = 1
money = 0

#Ask user for input
money = int(input("How much change do you need to make?"))

#Get the floor of the money to find how many coins you need and get the remainder and use it as the new amount of money for the next type of coin 
change_quarter = money // quarter
change_quarter_remainder = money % quarter

change_dime = change_quarter_remainder // dime
change_dime_remainder = money % dime

change_nickel = change_dime_remainder // nickel
change_nickel_remainder = money % nickel

change_penny = change_nickel_remainder // penny
change_penny_remainder = money % penny

#Print to the user
print(change quarter, "quarter(s)", change dime, "dime(s)", change nickel, "nickel(s)", change penny, "pennie(s)")

>Solution :

Your variables with _remainder are redundant.

change_quarter = money // quarter
money = money % quarter

change_dime = money // dime
money = money % dime

change_nickel = money // nickel
money = money % nickel

change_penny = money

Your solution with fix to make it clearer.

change_quarter = money // quarter
change_quarter_remainder = money % quarter

change_dime = change_quarter_remainder // dime
change_dime_remainder = change_quarter_remainder % dime

change_nickel = change_dime_remainder // nickel
change_nickel_remainder = change_dime_remainder % nickel

change_penny = change_nickel_remainder // penny
change_penny_remainder = change_nickel_remainder % penny

Leave a Reply