将数组添加在一起时只能将列表(而不是“int”)集中到列表

我有一个项目,我必须将学生的考试分添加到班级总分中,然后找到班级平均分。


AllStudents = []

Sum = 0

ClassSum = []

Total = []

for x in range(2):

    name = input("enter student name: ")


    Student = []

    Student.append(name)


    StudentPoint1 = int(input("points for test 1: "))

    if StudentPoint1 > 20:

        print("Test 1 score invalid, should be less than 20")


    StudentPoint2 = int(input("points for test 2: "))

    if StudentPoint2 > 25:

        print("Test 2 score invalid, should be less than 25")


    StudentPoint3 = int(input("points for test 3: "))

    if StudentPoint1 > 35:

        print("Test 3 score invalid, should be less than 35")


    Student.append(StudentPoint1)

    Student.append(StudentPoint2)

    Student.append(StudentPoint3)


    Sum = StudentPoint1 + StudentPoint2 + StudentPoint3

    Total.append(Sum)

    ClassSum.append(Total + Sum)


    AllStudents.append(Student)

print(ClassSum)

print(AllStudents)```

在显示 ClassSum.append(Total + Sum) 的行上,我收到错误“只能将列表(而不是“int”)集中到列表”


翻翻过去那场雪
浏览 79回答 3
3回答

温温酱

我不知道你到底想做什么,但我认为用“+=”递增比附加到列表更好。如果有帮助的话我编写了这段代码:AllStudents = []Sum = 0ClassSum = 0Total = []for x in range(2):    name = input("enter student name: ")    Student = []    Student.append(name)    StudentPoint1 = int(input("points for test 1: "))    if StudentPoint1 > 20:        print("Test 1 score invalid, should be less than 20")    StudentPoint2 = int(input("points for test 2: "))    if StudentPoint2 > 25:        print("Test 2 score invalid, should be less than 25")    StudentPoint3 = int(input("points for test 3: "))    if StudentPoint1 > 35:        print("Test 3 score invalid, should be less than 35")    Student.append(StudentPoint1)    Student.append(StudentPoint2)    Student.append(StudentPoint3)    Sum = StudentPoint1 + StudentPoint2 + StudentPoint3    ClassSum+=Sum    AllStudents.append(name)print(ClassSum)print(AllStudents)print(f'Average is {ClassSum/len(AllStudents)}')

牛魔王的故事

如果您尝试为每个学生添加总和,则您正在尝试对列表和整数变量求和,那么您需要使用列表索引进行访问。 ClassSum.append(Total[x] + Sum)

慕婉清6462132

尝试ClassSum.append(sum(Total))您不能使用整数和列表作为操作数执行加法。另外,为什么您要尝试添加Sum已经作为其元素的Total内容。TotalSum
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python