基本上它适用于我尝试过的几乎所有情况,除了 0.93。然后我在 while 循环中添加了“print money”,以查看它在每个循环之后做了什么,这就是发生的事情:
Enter an amount less than a dollar: 0.93
0.68
0.43
0.18
0.08
0.03
0.02
0.01
3.81639164715e-17
-0.01
Your change is 3 quarters 1 dimes 1 nickels 4 pennies
有人可以解释到底发生了什么吗?
money = input("Enter an amount less than a dollar: ")
quarter = 0
dime = 0
nickel = 0
penny = 0
while money > 0.00:
if money >= 0.25:
quarter = quarter + 1
money = money - 0.25
elif money >= 0.10:
dime = dime+1
money = money - 0.10
elif money >= 0.05:
nickel = nickel + 1
money = money - 0.05
else:
penny = penny + 1
money = money - 0.01
print "Your change is %d quarters %d dimes %d nickels %d pennies" % (quarter, dime, nickel, penny)