我现在正在学习Python(2.7),并且练习说写一个计算你需要支付特定金额的硬币数量的程序.我的解决方案是:
sum = input("Bitte gebe einen Euro Betrag ein: ")
coins = []
euro = [20,10,5,2,1,0.5,0.2,0.1,0.05,0.02,0.01]
for i in euro:
while sum >= i:
sum -= i
coins.append(i)
print coins
Run Code Online (Sandbox Code Playgroud)
这几乎可以工作,但是当我输入例如17,79时,它给了我17,78的硬币.
Bitte gebe einen Euro Betrag ein: 17.79
[10, 5, 2, 0.5, 0.2, 0.05, 0.02, 0.01]
Run Code Online (Sandbox Code Playgroud)
为什么?这与圆形有关吗?
我想用很长的数字分开计算.我怎么能在Python2.7中做到这一点?我想到以某种方式在数组中写入数字,以便我可以使用数组(x)访问数字:
number = 123456789123456789
array = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9]
Run Code Online (Sandbox Code Playgroud)
问题是,该数字有这么多位数,手动执行该操作需要很长时间.那我怎么能自动完成呢?