[英]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.