简体   繁体   English

IndexError:列表索引超出范围,即使打印单个列表和索引似乎没有错误

[英]IndexError: list index out of range even though printing individual lists and index doesn't seem to have an error

take = randint(0, len(teacherClass[teacher])-1)
print(take)
print(teacherClass)
print(teacher)
print(teacherClass[teacher])
triesDone = 0
while triesDone < len(teacherClass[teacher]):
    cp = teacherClass[teacher][take]
    if (cp not in (blocks[teacher][day])) and (blocksS[cp][day][block] == ""):
        blocks[teacher][day][block] = cp
        blocksS[cp][day][block] = teacherSub[teacher]
    take +=1
    triesDone += 1
    if take == len(teacherClass[teacher])-1:
        take = 0

When I run the program after some time, the above part is hit and the program starts working as intended but line 8 raises the error ("IndexError: list index out of range").当我在一段时间后运行程序时,上面的部分被命中,程序开始按预期工作,但第 8 行引发错误(“IndexError:列表索引超出范围”)。

Trying to solve that and understand the problem, I tried to print the entire dictionary(teacherClass) and the indices used(teacher and take) but even after that, it seems the line 8 should work.为了解决这个问题并理解问题,我尝试打印整个字典(teacherClass)和使用的索引(teacher and take),但即使在那之后,第 8 行似乎也应该工作。 Output I am getting: Output with list and index Output 我得到: Output 与列表和索引

Please help me understand the problem and a solution.请帮助我了解问题和解决方案。 Thanks谢谢

There is a possibility that take could be: len(teacherClass[teacher])-1 from the assignment on the first line.有一种take是: len(teacherClass[teacher])-1来自第一行的作业。 Later there is take += 1 .后来有take += 1 This mean that it is larger than the limit, so take = 0 is never executed.这意味着它大于限制,因此永远不会执行take = 0

Did you mean:你的意思:

if take >= len(teacherClass[teacher])-1:
    take = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到 IndexError: list index out of range 错误,即使我有一个正确的列表 - Why am I getting IndexError: list index out of range error even though I have a proper list IndexError:在矩阵打印中列出超出范围的索引 - IndexError: list index out of range in matrix printing Python,列出错误:IndexError:列表索引超出范围 - Python, Lists error: IndexError: list index out of range Python IndexError:列表索引超出范围没有意义 - Python IndexError: list index out of range doesn't make sense IndexError:列表索引超出范围,即使我没有引用大于列表长度的值? - IndexError: list index out of range even though I do not reference a value greater than the length of the list? Python中的索引超出范围错误(IndexError:列表索引超出范围) - Index out of range error in Python (IndexError: list index out of range) Python错误:IndexError列表索引超出范围 - Python Error: IndexError list index out of range 获取 IndexError:列表索引超出范围错误 - Getting IndexError: list index out of range error 正在获取错误:IndexError:列表索引超出范围&#39; - Getting error : IndexError: list index out of range' 此错误来自 IndexError: list index out of range - this error comes IndexError: list index out of range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM