Python input(): "NameError: name 'n' 未定义"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17413502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
input(): "NameError: name 'n' is not defined"
提问by Cal Courtney
Ok, so I'm writing a grade checking code in python and my code is:
好的,所以我正在用 python 编写成绩检查代码,我的代码是:
unit3Done = str(input("Have you done your Unit 3 Controlled Assessment? (Type y or n): ")).lower()
if unit3Done == "y":
pass
elif unit3Done == "n":
print "Sorry. You must have done at least one unit to calculate what you need for an A*"
else:
print "Sorry. That's not a valid answer."
When I run it through my python compiler and I choose "n"
, I get an error saying:
当我通过 python 编译器运行它并选择 时"n"
,我收到一条错误消息:
"NameError: name 'n' is not defined"
“NameError:名称'n'未定义”
and when I choose "y"
I get another NameError
with 'y'
being the problem, but when I do something else, the code runs as normal.
当我选择"y"
我再NameError
有'y'
是问题,但是当我做别的事情,代码运行正常。
Any help is greatly appreciated,
任何帮助是极大的赞赏,
Thank you.
谢谢你。
采纳答案by Ashwini Chaudhary
Use raw_input
in Python 2 to get a string, input
in Python 2 is equivalent to eval(raw_input)
.
使用raw_input
Python中2得到一个字符串,input
在Python 2相当于eval(raw_input)
。
>>> type(raw_input())
23
<type 'str'>
>>> type(input())
12
<type 'int'>
So, When you enter something like n
in input
it thinks that you're looking for a variable named n
:
因此,当您输入类似的内容时n
,input
它认为您正在寻找一个名为 的变量n
:
>>> input()
n
Traceback (most recent call last):
File "<ipython-input-30-5c7a218085ef>", line 1, in <module>
type(input())
File "<string>", line 1, in <module>
NameError: name 'n' is not defined
raw_input
works fine:
raw_input
工作正常:
>>> raw_input()
n
'n'
help on raw_input
:
帮助raw_input
:
>>> print raw_input.__doc__
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled. The prompt string, if given,
is printed without a trailing newline before reading.
help on input
:
帮助input
:
>>> print input.__doc__
input([prompt]) -> value
Equivalent to eval(raw_input(prompt)).
回答by Martijn Pieters
You are using the input()
functionon Python 2. Use raw_input()
instead, or switch to Python 3.
您正在Python 2 上使用该input()
函数。请raw_input()
改用,或切换到 Python 3。
input()
runs eval()
on the input given, so entering n
is interpreted as python code, looking for the n
variable. You could work around that by entering 'n'
(so with quotes) but that is hardly a solution.
input()
eval()
在给定的输入上运行,因此输入n
被解释为 python 代码,寻找n
变量。您可以通过输入'n'
(用引号引起来)来解决这个问题,但这几乎不是解决方案。
In Python 3, raw_input()
has been renamed to input()
, replacing the version from Python 2 altogether. If your materials (book, course notes, etc.) use input()
in a manner that expects n
to work, you probably need to switch to using Python 3 instead.
在 Python 3 中,raw_input()
已重命名为input()
,完全替换了 Python 2 中的版本。如果您的材料(书籍、课程笔记等)input()
以预期n
有效的方式使用,您可能需要改用 Python 3。