【发布时间】:2022-01-16 07:23:22
【问题描述】:
我有这个空列表,我想在主列表中添加另一个列表 这是我得到的错误:
Classroom = []
n = int(input("give me the numbers of the students : "))
for i in range(n) :
name = input("give me the name of the student : ")
score = float(input("give me the score of the student : "))
Classroom.append(list())
Classroom[i][0] = name
Classroom[i][1] = score
print(Classroom)
这是我遇到的错误
IndexError: list assignment index out of range
【问题讨论】:
-
Classroom[i]没有元素。所以访问Classroom[i][0]或Classroom[i][1]会抛出这个错误。也许你想append() -
这与嵌套无关,您不能将 any 列表索引分配到越界。
Classroom.append([name, score])?Classroom[i].append(name)?
标签: python variable-assignment