【发布时间】:2021-04-05 14:20:10
【问题描述】:
这是我的代码。解决方案 #1:
def age():
input('Age: ')
def friends():
print("John" + age)
预期结果:
John13
实际结果#1:
TypeError: can only concatenate str (not "function") to str
解决方案 #2:
def age():
input('Age: ')
def friend():
print("John" + age())
解决方案结果 #2:
TypeError: can only concatenate str (not "NoneType") to str
如何将我的函数age(age 是一个input() 函数声明)连接到一个或多个字符串? age 函数是一个 input(),我想将它添加到适当的名称或字符串中。
【问题讨论】:
-
您需要从
age()方法返回可以连接的值:return input('Age: ')。 -
age()函数不返回任何东西,因此它默认返回 None。
标签: python python-3.x