2016-02-12 68 views
1

我是python的新手,並且遇到輸入問題。 當我使用命令userName = input('What is your name? ') 它說是這樣的:Python試圖解析輸入

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

我必須用它做什麼?

+2

您正在使用哪個版本的Python?如果你使用Python 2,你應該調用'raw_input'而不是'input'。 –

+2

使用'raw_input()' – Andy

+3

[Input \'和\'raw \ _input \'之間的差異可能重複](http://stackoverflow.com/questions/3800846/differences-between-input-and-raw輸入) – Andy

回答

2

改變它來:

userName = raw_input('What is your name?') 

在Python 2.x的:

raw_input()返回字符串值和
input()嘗試來評估輸入作爲命令

但是在Python 3.X ,輸入已被廢棄,並且現在輸入以前稱爲raw_input的函數。

+2

'input'不返回整數值,它試圖評估輸入爲Python代碼。 –

+1

文檔說'相當於eval(raw_input(提示))',而不是'相當於int(raw_input(提示))'。 – zondo

+1

@VincentSavard沒錯!感謝您的更正。 – Jumayel

1

函數input()將輸入評估爲命令 - 這就是數字工作的原因,但不是無法執行的通用字符串。要實現將字符串附加到變量,請使用更通用的raw_input(),它將所有內容都讀取爲字符串。

新代碼看起來像

userName = raw_input('What is your name? ') 

運氣最好的,快樂編碼!