Python 在二维列表中添加列表
为什么会在下面抛出错误Python 在二维列表中添加列表,python,list,Python,List,为什么会在下面抛出错误 marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]] total = 0 for s in range(3): for m in range(5): total[s] = total[s] + marks [s][m] print("total for student",s,"is",total[s]) 我们可以简化您的代码并使其工作 代码: TypeError: 'int'
marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]]
total = 0
for s in range(3):
for m in range(5):
total[s] = total[s] + marks [s][m]
print("total for student",s,"is",total[s])
我们可以简化您的代码并使其工作 代码:
TypeError: 'int' object is not subscriptable
marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]]
total = 0
for s in range(len(marks)):
print("total for student",s,"is",sum(marks[s]))
total for student 0 is 58
total for student 1 is 35
total for student 2 is 68
输出:
TypeError: 'int' object is not subscriptable
marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]]
total = 0
for s in range(len(marks)):
print("total for student",s,"is",sum(marks[s]))
total for student 0 is 58
total for student 1 is 35
total for student 2 is 68
对代码进行更改
代码1:
TypeError: 'int' object is not subscriptable
marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]]
total = 0
for s in range(len(marks)):
print("total for student",s,"is",sum(marks[s]))
total for student 0 is 58
total for student 1 is 35
total for student 2 is 68
注意事项:
TypeError: 'int' object is not subscriptable
marks = [[12,14,8,7,17],[6,8,5,3,13],[16,15,9,10,18]]
total = 0
for s in range(len(marks)):
print("total for student",s,"is",sum(marks[s]))
total for student 0 is 58
total for student 1 is 35
total for student 2 is 68
- 由于您试图计算列表的总和,因此可以使用
方法sum
可能是因为
total
是一个int,你不能在int上调用[s]
…我是这样的,我该怎么办?用total+=marks[s][m]
来代替。这仍然会引发一个错误,只需将total更改为一个列表,total=[]
我希望学生0的总数为58学生1的总数为35学生2的总数为68@AlanRobinson我在这里给出的输出不是吗?月份=[“一月”、“二月”、“三月”、“四月”]温度=[]a=0,每个月:温度=int(输入(“请输入”,月份[a]))a=a+1@AlanRobinson我不能带你去那里