2014-05-05 78 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