![](/img/trans.png)
[英]typeerror unsupported operand type(s) for %: 'list' and 'int'
[英]TypeError: unsupported operand type(s) for +=: 'int' and 'list'
我正在尝试用 python 做一个项目。 我在行中遇到错误
s+=line
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
这是名为testIfCorrect
的函数:
def testIfCorrect(world, x, y):
s=0
for line in world:
s+=line
print("ligne",line)
if(s > 2):
return False
for i in range(x):
if(sum(returnColumn(world, i)) > 2):
return False
for j in range(x):
for k in range(y):
if(j == k):
pass
else:
if(world[j] == world[k]):
return False
if(returnColumn(world, j) == returnColumn(world ,k)):
return False
def returnColumn(array, column):
return [col[column] for col in array]
错误在哪里?
在
s=0
for line in world:
s+=line
这里s
是一个 int 而word
是 2D List。 因此,在for line in world
, line
是一1D List
。 不可能将List
添加到int
类型中。 这里, s+=line
不正确
因此,在s+=line
,您可以替换s+=sum(line) 。 我想你已经找到了答案。
尝试这个:
s=0
for line in world:
s+=sum(line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.