2014-11-21 74 views
0
varname = input("what is your name?: ") 
print ("Hello", varname) 

有沒有人知道爲什麼這不會工作?只是打印問候程序引發NameError

Traceback (most recent call last): 
    File "C:/Users/sfawcett/Desktop/python.py", line 1, in <module> 
    varname = input("what is your name?: ") 
    File "<string>", line 1, in <module> 
NameError: name 'Lewis' is not defined 
+0

我已經添加了蟒蛇2標籤。請糾正我,如果我錯了@ user3801278 – parchment 2014-11-21 09:16:45

+0

什麼版本的Python使用? – Hackaholic 2014-11-21 09:17:55

+0

把你的完整代碼 – 2014-11-21 09:18:08

回答

2

input()評估字符串。要獲取原始值,你應該使用raw_input()

+0

感謝您的幫助 – user3801278 2014-11-21 09:35:13

0

在python3: -

In [4]: varname = input("what is your name?: ") 
what is your name?: lewis 

In [5]: print ("Hello", varname) 
Hello lewis 
在python2

: -

閱讀here about input

In [185]: varname = raw_input("what is your name?: ") #don't use `input()` 
what is your name?: lewis 

In [186]: print ("Hello", varname) 
('Hello', 'lewis') 
相關問題