input() 错误 - NameError: name '...' 未定义 [英] input() error - NameError: name '...' is not defined
问题描述
当我尝试运行这个简单的脚本时出现错误:
input_variable = input("请输入您的姓名:")打印(你的名字是"+ input_variable)
假设我输入了dude",我得到的错误是:
第 1 行,在 中input_variable = input("请输入您的姓名:")文件<string>",第 1 行,在 <module> 中.NameError:名称dude"未定义
我正在运行 Mac OS X 10.9.1,我正在使用 Python 3.3 安装附带的 Python Launcher 应用程序来运行脚本.
TL;DR
Python 2.7 中的input
函数,将您输入的任何内容作为 Python 表达式进行评估.如果你只是想读取字符串,那么使用 Python 2.7 中的 raw_input
函数,它不会评估读取的字符串.
如果您使用的是 Python 3.x,raw_input
已重命名为 input
.引用 Python 3.0 发行说明、
raw_input()
重命名为 input()
.也就是说,新的 input()
函数从 sys.stdin
读取一行并返回它,并去掉尾部的换行符.如果输入过早终止,它会引发 EOFError
.要获得 input()
的旧行为,请使用 eval(input())
<小时>
在 Python 2.7 中,有两个函数可用于接受用户输入.一个是 input
另一个是raw_input
.你可以认为它们之间的关系如下
input = eval(raw_input)
考虑下面的一段代码以更好地理解这一点
<预><代码>>>>dude = "thefourtheye">>>input_variable = input("请输入您的姓名:")输入你的名字:老兄>>>输入变量'thefourtheye'input
接受来自用户的字符串并评估当前 Python 上下文中的字符串.当我输入 dude
作为输入时,它发现 dude
绑定到值 thefourtheye
,因此评估结果变成了 thefourtheye
并分配给 input_variable
.
如果我输入了当前 python 上下文中不存在的其他内容,它将失败 NameError
.
Python 2.7 的 input
的安全注意事项:
由于评估了任何用户类型,它也会带来安全问题.例如,如果您已经使用 import os
在程序中加载了 os
模块,然后用户输入
os.remove("/etc/hosts")
这将被python评估为函数调用表达式并被执行.如果您使用提升的权限执行 Python,/etc/hosts
文件将被删除.看,这有多危险?
为了演示这一点,让我们再次尝试执行 input
函数.
现在,当 input("Enter your name:")
被执行时,它等待用户输入并且用户输入是一个有效的 Python 函数调用,因此它也会被调用.这就是为什么我们会看到 再次输入您的名字:
再次提示.
所以,你最好使用 raw_input
函数,就像这样
input_variable = raw_input("请输入您的姓名:")
如果您需要将结果转换为其他类型,那么您可以使用适当的函数来转换raw_input
返回的字符串.例如,要将输入读取为整数,请使用 int
函数,如 this answer 中所示.
在 python 3.x 中,只有一个函数可以获取用户输入,即 input
,相当于Python 2.7的raw_input
.
I am getting an error when I try to run this simple script:
input_variable = input("Enter your name: ")
print("your name is" + input_variable)
Let's say I type in "dude", the error I am getting is:
line 1, in <module>
input_variable = input("Enter your name: ")
File "<string>", line 1, in <module>
NameError: name 'dude' is not defined
I am running Mac OS X 10.9.1 and I am using the Python Launcher app that came with the install of Python 3.3 to run the script.
TL;DR
input
function in Python 2.7, evaluates whatever your enter, as a Python expression. If you simply want to read strings, then use raw_input
function in Python 2.7, which will not evaluate the read strings.
If you are using Python 3.x, raw_input
has been renamed to input
. Quoting the Python 3.0 release notes,
raw_input()
was renamed toinput()
. That is, the newinput()
function reads a line fromsys.stdin
and returns it with the trailing newline stripped. It raisesEOFError
if the input is terminated prematurely. To get the old behavior ofinput()
, useeval(input())
In Python 2.7, there are two functions which can be used to accept user inputs. One is input
and the other one is raw_input
. You can think of the relation between them as follows
input = eval(raw_input)
Consider the following piece of code to understand this better
>>> dude = "thefourtheye"
>>> input_variable = input("Enter your name: ")
Enter your name: dude
>>> input_variable
'thefourtheye'
input
accepts a string from the user and evaluates the string in the current Python context. When I type dude
as input, it finds that dude
is bound to the value thefourtheye
and so the result of evaluation becomes thefourtheye
and that gets assigned to input_variable
.
If I enter something else which is not there in the current python context, it will fail will the NameError
.
>>> input("Enter your name: ")
Enter your name: dummy
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'dummy' is not defined
Security considerations with Python 2.7's input
:
Since whatever user types is evaluated, it imposes security issues as well. For example, if you have already loaded os
module in your program with import os
, and then the user types in
os.remove("/etc/hosts")
this will be evaluated as a function call expression by python and it will be executed. If you are executing Python with elevated privileges, /etc/hosts
file will be deleted. See, how dangerous it could be?
To demonstrate this, let's try to execute input
function again.
>>> dude = "thefourtheye"
>>> input("Enter your name: ")
Enter your name: input("Enter your name again: ")
Enter your name again: dude
Now, when input("Enter your name: ")
is executed, it waits for the user input and the user input is a valid Python function invocation and so that is also invoked. That is why we are seeing Enter your name again:
prompt again.
So, you are better off with raw_input
function, like this
input_variable = raw_input("Enter your name: ")
If you need to convert the result to some other type, then you can use appropriate functions to convert the string returned by raw_input
. For example, to read inputs as integers, use the int
function, like shown in this answer.
In python 3.x, there is only one function to get user inputs and that is called input
, which is equivalent to Python 2.7's raw_input
.
这篇关于input() 错误 - NameError: name '...' 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!