3 回答

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊
我不知道你到底想做什么,但我認(rèn)為用“+=”遞增比附加到列表更好。如果有幫助的話我編寫了這段代碼:
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貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果您嘗試為每個(gè)學(xué)生添加總和,則您正在嘗試對列表和整數(shù)變量求和,那么您需要使用列表索引進(jìn)行訪問。 ClassSum.append(Total[x] + Sum)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
嘗試
ClassSum.append(sum(Total))
您不能使用整數(shù)和列表作為操作數(shù)執(zhí)行加法。另外,為什么您要嘗試添加Sum
已經(jīng)作為其元素的Total
內(nèi)容。Total
Sum
添加回答
舉報(bào)