【发布时间】:2021-04-26 12:29:14
【问题描述】:
我正在尝试编写一个添加组合值的程序,但我收到此错误消息:
TypeError: unsupported operand type(s) for +: 'int' and 'tuple
这是我的代码:
from itertools import combinations
# values are 32, 20, 5, 18
objects = [
("golden purse", 32, 2),
("gold", 20, 5),
("iron statue", 5, 1),
("iron sword", 18, 6),
]
def combinaisons(l):
tmp = []
for i in range(1, len(l) + 1):
tmp += list(combinations(l, i))
return tmp
comb = combinaisons(objects)
def calc_values(l):
somme = 0
for c in range(len(l)):
for i in range(len(l[c])):
somme = somme + l[i][1]
print(somme)
calc_values(comb)
“l[i][1]”应该是32这样的整数,所以我看不出问题出在哪里
【问题讨论】:
-
“应该是”...你检查过它是吗?例如。只需
print之前就可以了吗? -
它说
tuple index out of range但我不知道 l[0][1] 是如何超出范围的.. -
好吧,如果你做了
print(l)...
标签: python list loops tuples range