为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

翻翻过去那场雪 2023-07-05 16:22:59
我有一个项目,我必须将学生的考试分添加到班级总分中,然后找到班级平均分。AllStudents = []Sum = 0ClassSum = []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”)集中到列表”
查看完整描述

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)}')


查看完整回答
反对 回复 2023-07-05
?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

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



查看完整回答
反对 回复 2023-07-05
?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

尝试

ClassSum.append(sum(Total))

您不能使用整数和列表作为操作数执行加法。另外,为什么您要尝试添加Sum已经作为其元素的Total内容。TotalSum


查看完整回答
反对 回复 2023-07-05
  • 3 回答
  • 0 关注
  • 78 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信