我是这个领域的新手,帮我找出错误
name = input( "Enter your name:" )
age = 12
print( "Hi, " + name + " " + age + " years old!" )
$ python test.py
Enter your name:evgen
Traceback (most recent call last):
File "test.py", line 5, in <module>
print( "Hi, " + name + " " + age + " years old!" )
TypeError: can only concatenate str (not "int") to str
发布于 2020-06-17 06:55:13
在连接之前,需要将int转换为str:
name = input( "Enter your name:" )
age = 12
print( "Hi, " + name + " " + str(age) + " years old!" )
还可以看看使用python进行字符串格式化:https://docs.python.org/3.4/library/string.html#format-examples
发布于 2020-06-17 06:55:42
您正在将字符串与整数age
连接起来。简单地将/convert age
转换为str
,如下所示:
print( "Hi, " + name + " " +str(age) + " years old!" )
https://stackoverflow.com/questions/62423047
复制