如果我尝试执行以下操作:
things = 5
print("You have " + things + " things.")
Run Code Online (Sandbox Code Playgroud)
我在Python 3.x中收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
Run Code Online (Sandbox Code Playgroud)
......以及Python 2.x中的类似错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
在Python中,以下代码会产生错误:
a = 'abc'
b = 1
print(a + b)
Run Code Online (Sandbox Code Playgroud)
(错误是"TypeError:无法连接'str'和'int'对象").
为什么Python解释器在遇到这些类型的串联时不会自动尝试使用str()函数?