[英]Python 3 unable to get letter input
I am using Enthought Canopy compiler. 我正在使用Enthought Canopy编译器。 I am trying to get a word input from user using this code below: 我正在尝试使用以下代码从用户那里输入单词:
starting_day = str(input("Enter the day you will be leaving: "))
however whenever I test the var by running the program I keep getting the error below after assigning a string of letters to the variable(it's runs when I give it numbers) 但是,每当我通过运行程序来测试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
You are running this with Python 2 , not Python 3. 您正在使用Python 2而不是Python 3运行它。
In Python 2, input()
passes all input to eval()
; 在Python 2中, input()
将所有输入传递给eval()
; the string monday
is interpreted as a Python name, which throws a NameError
because it is not defined. 字符串monday
被解释为Python名称,因为未定义,所以抛出NameError
。
You can see this in your traceback as well; 您也可以在回溯中看到这一点; exec
in Python 3 is a function , in Python 2 it is a statement. python 3中的exec
是一个函数 ,在Python 2中是一个语句。 The traceback uses it as a statement (no parenthesis). 追溯将其用作语句(无括号)。 Most of all, you are using Enthought Canopy, which can only support Python 2 . 最重要的是,您正在使用仅支持Python 2的 Enthought Canopy。
Either use raw_input()
instead, or run your code with an actual Python 3 interpreter. 请改用raw_input()
,或者使用实际的Python 3解释器运行代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.