如果我尝试执行以下操作:
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)
我怎样才能解决这个问题?
我尝试取一个整数并在其前面添加"b",将其转换为字符串时出现此错误:
File "program.py", line 19, in getname
name = "b" + num
TypeError: Can't convert 'int' object to str implicitly
Run Code Online (Sandbox Code Playgroud)
这与此功能有关:
num = random.randint(1,25)
name = "b" + num
Run Code Online (Sandbox Code Playgroud)