Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading