Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

python - TypeError: unsupported operand type(s) for +=: 'int' and 'list'

I am trying to do a project in python. I'm getting an error in the line

s+=line

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

Here is the function called 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]

Where is the error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In

s=0
for line in world:
    s+=line

Here s is an int and wordis 2D List. So, In for line in world, line is a 1D List. It is impossible to add a List into a int type. Here, s+=line in incorrect

So, In s+=line, you can replace s+=sum(line). I think you have found your answer.

Try this:

s=0
for line in world:
    s+=sum(line)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...