将数组加在一起时只能集中列表(不能"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"(集中到列表";

我不知道你到底想做什么,但我认为用'+='递增比附加到列表更好。我做了这个代码,如果它可以帮助:

AllStudents = []
Sum = 0
ClassSum = 0
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
ClassSum+=Sum
AllStudents.append(name)
print(ClassSum)
print(AllStudents)
print(f'Average is {ClassSum/len(AllStudents)}')

尝试

ClassSum.append(sum(Total))

不能将整数和列表作为操作数进行加法运算。还有,当Total已经有Sum作为其元素时,为什么要尝试将Sum添加到Total

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

最新更新