来自 Python input() 函数的 NameError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16457441/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:40:57  来源:igfitidea点击:

NameError from Python input() function

pythonpython-2.5

提问by Deepak Dubey

input_var = input ("Press 'E' and 'Enter' to Exit: ")

NameError: name 'e' is not defined

I am using Python 2.5. How I can overcome this error?

我正在使用 Python 2.5。我怎样才能克服这个错误?

采纳答案by poolie

inputreads and evaluates a Python expression. When it tries to evaluate it, it looks for a variable e, which is not defined, and fails.

input读取并计算 Python 表达式。当它试图评估它时,它会寻找一个e未定义的变量,但失败了。

You almost always want to use raw_inputinstead. (And in Python3, inputhas this behaviour.)

你几乎总是想用它raw_input来代替。(在 Python3 中,input有这种行为。)

Or, better, on Unix, use readlineso the user can edit their input.

或者,在 Unix 上使用readline更好,以便用户可以编辑他们的输入。