Python不会告诉我的名字。我该怎么让它告诉我的名字?

2024-04-24 00:44:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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'

当我在问号后面输入我的名字时,我也想要一个空格。你知道吗


Tags: namemostinputyoursoiscallwhat
1条回答
网友
1楼 · 发布于 2024-04-24 00:44:39

您需要先构建字符串,然后将结果传递给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 ^

相关问题 更多 >