[英]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.