我如何在 Python 中创建嵌套列表

How do i create a nested list in Python

我有这个空列表,我想在主列表中添加另一个列表 这是我收到的错误:

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 = []

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 : "))
    l=[]
    Classroom.append(l)
    Classroom[i].append(name)
    Classroom[i].append(score)

print(Classroom)

错误是您试图分配一个不存在的列表元素,请使用追加添加名称和分数