TypeError: Can only concatenate str (not “int”) to str (simple Python programme)

后端 未结 11 660
逝去的感伤
逝去的感伤 2020-12-07 05:17

I wrote this simple code and tried to execute in Windows 10 CMD ... and it gets the error message :

TypeError: Can only concatenate str (not \"int\") to str
         


        
11条回答
  •  一个人的身影
    2020-12-07 06:09

    Python is strongly typed, so it does not do type coercion unless you tell it to.

    You get that error if you try to add a number to a string, because based on the first operand it figures you want to concatenate strings.

    If you try to add a string to a number, you get “unsupported operand types” instead, but it’s the same problem.

    If you want to turn a number-in-a-string into an int you can add, use int(). If you want to turn a number value into a string you can concatenate, use str().

提交回复
热议问题