2017-09-25 68 views
0

我正在做一個簡單的程序,重複我輸入的內容。當前的代碼是這樣的:輸入undefined | python2.7

print("Please enter your username.") 
n = str(input(">> ")) 
print("Welcome, ",n) 

然而,當我運行它,並輸入,比方說,約翰,它將打印錯誤:約翰是不確定的,或者說非常相似。任何想法爲什麼?解決方案?

+3

可能重複[input()error - NameError:name'...'未定義](https://stackoverflow.com/questions/21122540/input-error-nameerror-name-is-not-defined ) – Carcigenicate

回答

2

改爲使用raw_input()。 使用input()需要在輸入名稱時使用「」,並且希望它被解釋爲字符串。

>>> n = input(">> ") 
>> "john" 
>>> print n 
john 

當使用raw_input()你可以做到以下幾點:

>>> n = raw_input(">> ") 
>> john 
>>> print n 
john 

input()解釋引號的字符串輸入作爲一個變量,即你可以做這樣的事情

>>> x = 5 
>>> y = input() 
>> x 
>>> print y 
5 

參見https://www.python-course.eu/input.php更多信息。