3 回答
TA贡献1752条经验 获得超4个赞
我不知道你到底想做什么,但我认为用“+=”递增比附加到列表更好。如果有帮助的话我编写了这段代码:
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)}')
TA贡献1830条经验 获得超3个赞
如果您尝试为每个学生添加总和,则您正在尝试对列表和整数变量求和,那么您需要使用列表索引进行访问。 ClassSum.append(Total[x] + Sum)
TA贡献1804条经验 获得超2个赞
尝试
ClassSum.append(sum(Total))
您不能使用整数和列表作为操作数执行加法。另外,为什么您要尝试添加Sum
已经作为其元素的Total
内容。Total
Sum
添加回答
举报