![](/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.