[英]Python won't tell my name. How can I make it to tell my name?
Name = input ("Hey, what's your name ?")
print ("So, your name is") + Name
结果是:
Hey, what's your name ?Robert
So, your name is
Traceback (most recent call last):
File "C:/Users/Angel'94/Desktop/Sal.py", line 2, in <module>
print ("So, your name is") + Name
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
当我在问号后输入我的名字时,我也想要一个空格。
您需要先构建字符串,然后将结果传递给print()
:
print("So, your name is " + Name)
您所做的是首先打印"So, your name is"
,并且print()
函数在完成后始终返回None
。 然后您尝试将Name
添加到None
返回值。
您可以将Name
作为额外参数传递给print()
函数,而不是使用串联:
print("So, your name is", Name)
并且该函数将为您在两个参数之间插入一个空格。
要在提示上获得一个空格,只需将其添加到input()
参数中:
Name = input("Hey, what's your name? ")
# extra space here ^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.