Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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?

like image 352
Julien Martin Avatar asked Sep 12 '15 19:09

Julien Martin


People also ask

How do I fix unsupported operand type s?

The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num .

How do you fix unsupported operand type S for NoneType and int?

The Python "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'" occurs when we try to use the addition (+) operator with a None value. To solve the error, figure out where the variable got assigned a None value and correct the assignment.

How do you fix unsupported operand type S for STR and STR?

The Python "TypeError: unsupported operand type(s) for -: 'str' and 'str'" occurs when we try to use the subtraction - operator with two strings. To solve the error, convert the strings to int or float values, e.g. int(my_str_1) - int(my_str_2) .

What does it mean unsupported operand type S for +: int and STR?

The Python "TypeError: unsupported operand type(s) for +: 'int' and 'str'" occurs when we try to use the addition (+) operator with an integer and a string. To solve the error, convert the string to an integer, e.g. my_int + int(my_str) .


1 Answers

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)
like image 118
Sakib Ahammed Avatar answered Oct 04 '22 07:10

Sakib Ahammed