1

I need help with my code. (Using vscode) I'm new to coding and thought a change calculator would be cool to make. So, after I made one, I ran into a problem.

The hundreds, fifties, twenties, tens, fives, and ones all work fine but when I try decimals like .03, my code just doesn't respond. Please help me. Here is the code.

Change = float(input('Change: '))

Hundreds, Fifties, Twenties, Tens, Fives, Ones = [0,0,0,0,0,0]
Quarters, Dimes, Nickels, Pennies = [0,0,0,0]

_ = True

while _:
    if Change <= 0:
        _ = False
        
    elif Change - 100 >= 0:
        Hundreds += 1
        Change -= 100
        
    elif Change - 50 >= 0:
        Fifties += 1
        Change -= 50
        
    elif Change - 20 >= 0:
        Twenties += 1
        Change -= 20
            
    elif Change - 10 >= 0:
        Tens += 1
        Change -= 10
        
    elif Change - 5 >= 0:
        Fives += 1
        Change -= 5
        
    elif Change - 1 >= 0:
        Ones += 1
        Change -= 1
        
    elif Change - .25 >= 0:
        Quarters += 1
        Change -= .25
            
    elif Change - .10 >= 0:
        Dimes += 1
        Change -= .10
        
    elif Change - .05 >= 0:
        Nickels +=1
        Change -= .05
            
    elif Change - .01 >= 0:
        Pennies += 1
        Change -= .01

Currency = [Hundreds, Fifties, Twenties, Tens, Fives, Ones, Quarters, Dimes, Nickels, Pennies]
ids = ['Hundreds: ', 'Fifties: ', 'Twenties: ', 'Tens: ', 'Fives: ', 'Ones: ', 'Quarters: ', 'Dimes: ', 'Nickels: ', 'Pennies: ']

print('')

for l in range(10):
    if Currency[l-1] > 0:
        print(ids[l-1] + str(Currency[l-1]))

print('')
Chooga S
  • 11
  • 1
  • 3
    You're running into the usual floating-point inaccuracies: if you start with `.03` (actually, an approximation to that), and subtract `.01` from it twice, you're left with `0.009999999999999997` rather than `0.01`. This triggers none of your conditions, so the loop runs forever. – jasonharper Dec 06 '22 at 19:54
  • 1
    Using floats and doing direct comparisons is going to be problematic due to rounding errors. https://stackoverflow.com/questions/588004/is-floating-point-math-broken?page=2&tab=scoredesc – Random Davis Dec 06 '22 at 19:54
  • 1
    BTW, it's more common and understandable to write `if Change >= 100:` than `if Change - 100 >= 0:` – Barmar Dec 06 '22 at 19:55

1 Answers1

0

The issue you are having is due to the operation of float() in python. Floats give you an approximation of the number

>>> num = float(.3)
>>> num - .25
0.04999999999999999 

I modified your code and this worked for me with .3

from decimal import Decimal

Change = Decimal(input("Change: "))

Hundreds, Fifties, Twenties, Tens, Fives, Ones = [0, 0, 0, 0, 0, 0]
Quarters, Dimes, Nickels, Pennies = [0, 0, 0, 0]

_ = True

while _:
    if Change <= 0:
        _ = False

    elif Change - 100 >= 0:
        Hundreds += 1
        Change -= 100

    elif Change - 50 >= 0:
        Fifties += 1
        Change -= 50

    elif Change - 20 >= 0:
        Twenties += 1
        Change -= 20

    elif Change - 10 >= 0:
        Tens += 1
        Change -= 10

    elif Change - 5 >= 0:
        Fives += 1
        Change -= 5

    elif Change - 1 >= 0:
        Ones += 1
        Change -= 1

    elif Change - Decimal('0.25') >= 0:
        Quarters += 1
        Change -= Decimal('.25')

    elif Change - Decimal('0.10') >= 0:
        Dimes += 1
        Change -= Decimal('0.10')

    elif Change - Decimal('0.05') >= 0:
        Nickels += 1
        Change -= Decimal('0.05')

    elif Change - Decimal('0.01') >= 0:
        Pennies += 1
        Change -= Decimal('0.01')

Currency = [
    Hundreds,
    Fifties,
    Twenties,
    Tens,
    Fives,
    Ones,
    Quarters,
    Dimes,
    Nickels,
    Pennies,
]
ids = [
    "Hundreds: ",
    "Fifties: ",
    "Twenties: ",
    "Tens: ",
    "Fives: ",
    "Ones: ",
    "Quarters: ",
    "Dimes: ",
    "Nickels: ",
    "Pennies: ",
]

print("")

for l in range(10):
    if Currency[l - 1] > 0:
        print(ids[l - 1] + str(Currency[l - 1]))

print("")
Change: .3

Quarters: 1
Nickels: 1

Here's another post that discusses the same problem - Python error in basic subtraction?