0

I need to create a script that asks the user for a $ amount and then outputs the minimum amount of coins to create that $ amount using quarters, dimes, nickels, and pennies.

mTotal = (float(input("Enter the amount you are owed in $:")))
numCoins = 0
while (mTotal != 0):
    if ((mTotal - 0.25) >= 0):
        mTotal-=0.25
        numCoins += 1
    elif ((mTotal - 0.10)>= 0):
        mTotal-=0.10
        numCoins += 1
    elif ((mTotal - 0.05)>= 0):
        mTotal-=0.05
        numCoins += 1
    elif ((mTotal - 0.01)>= 0):
        mTotal-=0.01
        numCoins += 1
print("The minimum number of coins the cashier can return is:", numCoins)

For some reason it only works if I enter 0.01, 0.05, 0.10 or an exact multiple of 0.25, otherwise the while loop goes on forever.

k lol
  • 3
  • 1

2 Answers2

0

Thanks guys! I managed to fix it by multiplying the input by 100 and turning it into an integer.

userInput = (float(input("Enter the amount you are owed in $:")))
mTotal = int(userInput*100)
numCoins = 0
while (mTotal != 0):
    if ((mTotal - 25) >= 0):
        mTotal-=25
        numCoins += 1
    elif ((mTotal - 10)>= 0):
        mTotal-=10
        numCoins += 1
    elif ((mTotal - 5)>= 0):
        mTotal-=5
        numCoins += 1
    elif ((mTotal - 0.01)>= 0):
        mTotal-=1
        numCoins += 1
print("The minimum number of coins the cashier can return is:", numCoins)
k lol
  • 3
  • 1
0

You can do: round((float(input("Enter the amount you are owed in $:"))))

The problem is that when you cast to a float, the string to float conversion will not be 100% accurate. For example if you were to type in 1.17 and cast that to a float, it would be be something like 1.1699999999999999.

S. Carr
  • 149
  • 1
  • 8