-2
factor = int(input("Which table would you like: "))
timestable = ([0,0])
for count in range(1,13):
timestable.append([0,0])
result = factor * count
timestable[count][0] = count
timestable [count][1] = result
for row in timestable:
print(row)
这是一个程序,它允许用户输入一个时间表并打印出1 *号码为12 *的数字。但每当我去运行代码,我得到这个错误信息:错误消息TypeError:'int'对象不支持项目分配
timestable[count][0] = count
TypeError: 'int' object does not support item assignment
有谁知道我必须在我的代码改变?
您初始化timestable是一个2元素的列表(括号没有做任何事情在这里),然后在循环的每一步中追加2元素列表。因此,timestable变成类似'[0,0,[0,0],[0,0],[0,0],...]'。你要么寻找'extend',要么想要将timestable初始化为'timestable ='[[0,0]]'。 – Evert