python at while loop IndexError:列表索引超出范围

李泰林
def solution(progresses, speeds):
    answer = []
    while len(progresses) != 0:
        count = 0
        for i in range(len(progresses)):
            if progresses[i] < 100:
                progresses[i] += speeds[i]
        while (progresses[0] >= 100) and (len(progresses) != 0):
            print("pop: ", progresses.pop(0))
            speeds.pop(0)
            count += 1
        if count != 0:
            answer.append(count)

    return answer


solution([93, 30, 55], [1, 30, 5])

我的 python 终端向我显示了这样的错误。

    while (progresses[0] >= 100) and (len(progresses) != 0):
IndexError: list index out of range

我不知道为什么会出现这个错误.....帮帮我

uccz

正如其他评论中提到的,您应该避免修改在whileorfor循环检查中使用的列表。


如果您想了解代码失败的原因。问题是这一行:

while (progresses[0] >= 100) and (len(progresses) != 0):

您正在访问元素零 ( progresses[0]) 但列表为空。如果交换这两个检查,它工作正常(因为如果第一个检查已经返回false,则根本不执行第二个检查)。

while (len(progresses) != 0 and progresses[0] >= 100):

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

IndexError:列表索引超出范围(Python)

While循环-IndexError:列表分配索引超出范围

while循环-IndexError:列表索引超出范围

python列表中的IndexError-“列表索引超出范围”

Python,列表错误:IndexError:列表索引超出范围

Python CSV:IndexError:列表索引超出范围

IndexError:列表索引超出范围python 3.6

IndexError:列表分配索引超出范围,Python

Python 单元测试 - IndexError:列表索引超出范围

IndexError:列表索引超出范围-Python CSV

IndexError:Python 3中的列表索引超出范围

IndexError:列表索引超出范围python(烧瓶)

Python-IndexError:列表索引超出范围

Python请求:“ IndexError:列表索引超出范围”

IndexError:列表索引超出范围错误python

python xpath IndexError:列表索引超出范围

IndexError:列表索引超出范围(Python 2.7)

Python 3:IndexError:列表索引超出范围

Python DJANGO错误IndexError在/列表索引超出范围

IndexError:列表索引超出范围 - Python 3.5.2

Python 网页抓取“IndexError:列表索引超出范围”

Python Web抓取“ IndexError:列表索引超出范围”

IndexError:在python列表中列出索引超出范围

Python 3.7.2:IndexError:列表索引超出范围

获取IndexError:在python中列表分配索引超出范围

Python:IndexError:列表索引超出范围:无法确定问题

IndexError:列表索引超出范围python 2.7

IndexError:列表索引超出范围(Python网络抓取)

IndexError:列表索引超出范围Python参数输入