2013-01-17 101 views
3

當使用Python3,做作爲報價在輸入蟒蛇

x=input("Enter your name: ") 

print (x) 

簡單的東西,並試圖運行它,用戶必須輸入他們的名字爲「史蒂夫」,而不僅僅是史蒂夫。

有沒有辦法輸入報價?

回答

11

我認爲你錯了。在Python 3,你需要引號:

localhost-2:~ $ python3.3 
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 01:25:11) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> x = input("Enter your name:") 
Enter your name:Steve 
>>> x 
'Steve' 

你會在Python 2的昔日,因爲input基本上eval就是你給它:

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

等你會使用raw_input代替:

>>> x = raw_input("Enter your name:") 
Enter your name:Steve 
>>> x 
'Steve' 

但是在Python 3,input是什麼raw_input曾經是,因此不需要引號。

+0

它可能是我的編碼設置,但是當我使用Python啓動程序啓動它時,然後它通過我的Mac上的terminal.app運行,如果我不使用引號,它會[失敗](http:// i .imgur.com/cfKPD.png?1)。 但是如果在使用IDLE時運行它,我不需要引號。 – ksg

+0

@Greenlep:加入'import sys'; 'print(sys.version)'到程序的開頭。很確定你不會看到3. *。 – DSM

+1

我的天啊,我是個白癡。謝謝,終端有一個過時的python版本... [2.7.1](http://i.imgur.com/5gKTm.png) 謝謝。 – ksg