【发布时间】:2020-05-09 00:14:50
【问题描述】:
def oburparaust(liste, money):
moneycontrol = 0.0
coin=0
control = 0
while True:
if liste[control] > money:
control+=1
else:
moneycontrol+=liste[control]
coin+=1
if moneycontrol==money:
break
elif moneycontrol > money:
moneycontrol-=liste[control]
coin-=1
print(moneycontrol)
if control>len(liste)-1:
print(moneycontrol)
control=len(liste)-1
else:
control+=1
#print(coin)
oburparaust([1.0, 0.50, 0.25, 0.10, 0.05, 0.01], 5.38)
我想计算 5.38 的钱用了多少硬币。但是我得到了范围错误的索引,我找不到为什么我要遍历列表?
错误;
回溯(最近一次通话最后): 文件“xxxx”,第 26 行,在 oburparaust([1.0, 0.50, 0.25, 0.10, 0.05, 0.01], 5.38) 文件“xxxx”,第 6 行,在 oburparaust 如果 liste[控制] > a: IndexError:列表索引超出范围 金钱控制:1.0 硬币:1 金钱控制:2.0 硬币:2 金钱控制:3.0 硬币:3 货币控制:4.0 硬币:4 金钱控制:5.0 硬币:5 金钱控制:5.0 硬币:5 金钱控制:5.0 硬币:5 金钱控制:5.25 硬币:6 金钱控制:5.25 硬币:6 金钱控制:5.35 硬币:7 金钱控制:5.35 硬币:7 金钱控制:5.35 硬币:7 金钱控制:5.359999999999999 硬币:8 金钱控制:5.369999999999999 硬币:9 货币控制:5.379999999999999 硬币:10 货币控制:5.379999999999999 硬币:10【问题讨论】:
-
在第 6 行
if liste[control] > money:它循环并尝试使用键 6 获取项目,该键不存在于数组中。 -
把这个
while True:改成这个while True and control < len(liste): -
@komatiraju032 提示:
True and watever等于watever -
感谢 komatiraju032
标签: python python-3.x algorithm