2014-05-05 76 views
0

我正在使用Enthought Canopy編譯器。我想用下面這段代碼以獲得來自用戶的一個字輸入:Python 3無法獲得字母輸入

starting_day = str(input("Enter the day you will be leaving: ")) 
然而

每當我通過運行指定的字母串的變量(它的運行後,我不斷收到以下錯誤的程序測試VAR當我給它數)

Enter the day you will be leaving: monday 

NameError         Traceback (most recent call last) 

C:\Users\user\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.1.0.1371.win-x86\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc) 
    174    else: 

    175     filename = fname 

--> 176    exec compile(scripttext, filename, 'exec') in glob, loc 

    177  else: 

    178   def execfile(fname, *where): 

c:\users\user\appdata\local\temp\tmpnwkfhu.py in <module>() 

----> 1 starting_day = str(input("Enter the day you will be leaving: ")) 

     2 

     3 lenght_of_stay = int(input("Enter the number of days you will say for: ")) 

     4 

     5 print(starting_day, lenght_of_stay) 

<string> in emulated_input(prompt) 

<string> in <module>() 

NameError: name 'monday' is not defined 
+0

如何將其更改爲3 – user133745

+0

您可能會發現[此問題](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid - 響應)在接受用戶輸入方面有用。 – jonrsharpe

+0

@ user133745:請不要在評論中提問,請改爲提問或[請教新問題](http://stackoverflow.com/questions/ask) – jfs

回答

1

您與的Python 2運行此,而不是Python 3

在Python 2,input()通過了所有輸入到eval();字符串monday被解釋爲Python名稱,因爲它未定義,所以會拋出NameError

你可以在你的回溯中看到這個; Python 3中的exec是一個函數,在Python 2中它是一個聲明。回溯使用它作爲語句(無括號)。最重要的是,你正在使用Enthought Canopy,其中can only support Python 2

改爲使用raw_input(),或者使用實際的Python 3解釋器運行代碼。

+0

如何更改爲py 3 – user133745

+0

@ user133745:您無法使用Canopy。安裝不同的Python 3發行版。 –

+0

爲什麼traceback中的py3compat.pyc文件中有'def execfile()',如果它不支持Python 3? – jfs