0

Pretty sure my problem has nothing to do with the floats, I don't see why a float would affect total > 0. I'm just trying to make a simple program for beginners for my computer programming class. I am looking for why inputting certain numbers for the coins doesn't output the twenties tens fives etc... one that does work is 50 for all inputs haven't found another one that does work there is no error message, it just doesn't print the output twenties, tens, fives etc.. Maybe I'm just excruciating tired but I just can't seem to find what's wrong with my program. Any help would be appreciated.

quarters = int(input("Enter a number of quarters: "))
dimes = int(input("Enter a number of dimes: "))
nickels = int(input("Enter a number of nickels: "))
pennies = int(input("Enter a number of pennies: "))

total = (quarters * .25) + (dimes * .1) + (nickels * .05) + (pennies * .01)
print("The total value of all coins is: " + str(total))
quarters = 0
nickels = 0
pennies = 0
dimes = 0
twenties = 0
tens = 0
fives = 0
ones = 0

while total > 0:
    while total >= 20:
        total = total - 20
        twenties = twenties + 1
    while total >= 10:
        total = total - 10
        tens = tens + 1
    while total >= 5:
        total = total - 5
        fives = fives + 1
    while total >= 1:
        total = total - 1
        ones = ones + 1
    while total >= .25:
        total = total - .25
        quarters = quarters + 1
    while total >= .1:
        total = total - .1
        dimes = dimes + 1
    while total >= .05:
        total = total - .05
        nickels = nickels + 1
    while total >= .01:
        total = total - .01
        pennies = pennies + 1
print("\nTwenties: " + str(twenties))
print("Tens: " + str(tens))
print("fives: " + str(fives))
print("ones: " + str(ones))
print("quarters: " + str(quarters))
print("dimes: " + str(dimes))
print("nickels: " + str(nickels))
print("pennies: " + str(pennies))

input("\nPress enter to quit")
  • print values in `while` loops and see how they change and when they change different than you expect. Or maybe you have problem with `float` values ie. for `float` this is not true: `0.1 + 0.2 == 0.3` - so maybe remove `while total > 0:` – furas Oct 15 '16 at 02:59
  • It seems odd that you inform us of an input that does work but not one that doesn't. In any event -- why are you using repeated subtraction rather than division? – John Coleman Oct 15 '16 at 03:00
  • 1
    Store and compute the currency values in pennies. – Kenny Ostrom Oct 15 '16 at 03:00
  • Part of writing a good question is "Help others reproduce the problem". What input didn't work for you and what error message did you get? – Paul Oct 15 '16 at 03:00
  • it doesn't work for 20 in all inputs - it gives sum 8.2 but it is float number `8.199999999999999289457264239899814128875732421875000000` - try `print("{:.54f}".format(8.2))` – furas Oct 15 '16 at 03:03
  • try to use only `int` values - instead of `20, 10, 5, 1, .25, .1, .05, .01` use values multiplied by 100 - `2000, 1000, 500, 100, 25, 10, 5, 1` - so you will work only with `int` values. And display `total/100`. – furas Oct 15 '16 at 03:10

0 Answers0