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

前端 未结 1 835
感动是毒
感动是毒 2020-12-17 18:50

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 +=:

相关标签:
1条回答
  • 2020-12-17 19:10

    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)
    
    0 讨论(0)
提交回复
热议问题