2015-07-01 111 views
0

這裏是我的代碼:故障運行Python腳本

name = input("What's your name? ") 
print("Nice to meet you " + name + "!") 

但是,當我輸入一個名稱,例如 「約翰」,它給了我這個錯誤:

Traceback (most recent call last): 
    File "name.py", line 1, in <module> 
    name = input("What's your name? ") 
    File "<string>", line 1, in <module> 
NameError: name 'john' is not defined 

版本我使用的蟒蛇是「2.7.10」。

回答

2

您需要使用raw_input而不是input,因爲後者會嘗試評估您輸入的內容,而前者會將其保留爲字符串。

2

Python 2的一個問題是,它試圖從input函數中獲取用戶輸入並將其作爲Python代碼執行,而不是將其作爲字符串返回。爲避免此問題,請使用raw_input代替:

name = raw_input("What's your name? ") 
+0

謝謝,第一次聽到'raw_input()'! –