[英]Why am i getting index of the range error?
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)
I want to calculate how much coin was used for the money of 5.38.我想计算 5.38 的钱用了多少硬币。 but i get index of the range error i couldn't find why where am i going over the list?
但是我得到了范围错误的索引,我找不到为什么我要遍历列表?
Error;错误;
Traceback (most recent call last): File "xxxx", line 26, in oburparaust([1.0, 0.50, 0.25, 0.10, 0.05, 0.01], 5.38) File "xxxx", line 6, in oburparaust if liste[kontrol] > a: IndexError: list index out of range moneycontrol: 1.0 Coin: 1 moneycontrol: 2.0 Coin: 2 moneycontrol: 3.0 Coin: 3 moneycontrol: 4.0 Coin: 4 moneycontrol: 5.0 Coin: 5 moneycontrol: 5.0 Coin: 5 moneycontrol: 5.0 Coin: 5 moneycontrol: 5.25 Coin: 6 moneycontrol: 5.25 Coin: 6 moneycontrol: 5.35 Coin: 7 moneycontrol: 5.35 Coin: 7 moneycontrol: 5.35 Coin: 7 moneycontrol: 5.359999999999999 Coin: 8 moneycontrol: 5.369999999999999 Coin: 9 moneycontrol: 5.379999999999999 Coin: 10 moneycontrol: 5.379999999999999 Coin: 10
You have list "liste" with 6 items, what means that your index starts with 0 and ends with 5, in the point when your variable control will have value 6, index will be out of the range.您有包含 6 个项目的列表“liste”,这意味着您的索引以 0 开头并以 5 结尾,当您的变量控件的值为 6 时,索引将超出范围。 you will need to control that variable "control" if it is less than length of your list "liste".
如果它小于列表“liste”的长度,您将需要控制该变量“control”。 Something like:
就像是:
def oburparaust(liste, money):
moneycontrol = 0.0
coin=0
control = 0
while control < len(liste):
. . .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.