【发布时间】:2020-08-03 20:29:26
【问题描述】:
这个代码应该给我最少数量的硬币(硬币、硬币、镍和便士),加起来就是所欠的金额。 当我输入 0.25 的倍数的值时,它可以无缝工作。但是当我在终端中输入其他值时,它只是插入一个新行而不做任何事情。我怎么搞砸了?
owed = float(input("how much change is owed?"))
coins = 0
if owed % 0.25 == 0:
coins = owed / 0.25
print(int(coins))
exit()
elif owed % 0.25 != 0:
while owed > 0:
if (owed - 0.25) >= 0:
coins += 1
owed -= 0.25
elif (owed - 0.10) >= 0:
coins += 1
owed -= 0.10
elif (owed - 0.05) >= 0:
coins += 1
owed -= 0.05
elif (owed - 0.01) >= 0:
coins += 1
owed -= 0.01
print(int(coins))
exit()
【问题讨论】:
-
以下两个答案都建议保留
float以获取输入(GrandPhuba 至少只使用一次)。一般来说,我建议直接进入 int,如果需要,请执行"".split('.')并将美元乘以 100 以获得美分。
标签: python python-3.x while-loop