Python 类型错误:+= 不支持的操作数类型:'int' 和 'list'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32542589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
提问by Julien Martin
Brand new to Python (and coding altogether). I have to do a project for my school. I'm having an issue with one of the functions. The line "s+=line" is giving me:
全新的 Python(以及完全编码)。我必须为我的学校做一个项目。我遇到了其中一项功能的问题。“s+=line”这一行给了我:
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
类型错误:+= 不支持的操作数类型:'int' 和 'list'
So I take it my function called "testIfCorrect" is wrong, but I'm not really sure what the problem is...
所以我认为我的名为“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]
Thank you and sorry for my bad english
谢谢你,对不起我的英语不好
采纳答案by Sakib Ahammed
In
在
s=0
for line in world:
s+=line
Here S
is an int and word
is 2D List. So, In for line in world
, line is a List. So, line is not add with int s ie. s+=line
in wrong
这S
是一个 int 和word
2D 列表。所以, In for line in world
, line 是一个列表。所以, line 不是用 int s 添加的,即。s+=line
在错
So, In s+=line
, you can replace s+=sum(line). I think you find your answer.
因此, In s+=line
,您可以替换s+=sum(line)。我想你找到了答案。
Try this:
尝试这个:
s=0
for line in world:
s+=sum(line)