input()错误-NameError:未定义名称'...' [英] input() error - NameError: name '...' is not defined

查看:390
本文介绍了input()错误-NameError:未定义名称'...'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试运行此简单脚本时出现错误:

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

我正在使用Python 2.7运行这些脚本.

I am running these scripts with Python 2.7.

推荐答案

TL; DR

input函数将输入的内容评估为Python表达式.如果您只想读取字符串,请在Python 2.7中使用raw_input函数,该函数不会评估读取的字符串.

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.

如果使用的是Python 3.x,则raw_input已重命名为input.引用 Python 3.0发行说明

If you are using Python 3.x, raw_input has been renamed to input. Quoting the Python 3.0 release notes,

raw_input()重命名为input().也就是说,新的input()函数从sys.stdin读取一行,并以结尾的换行符去除后返回它.如果输入过早终止,则它会升高EOFError.要获取input()的旧行为,请使用eval(input())

raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input())


在Python 2.7 中,有两个函数可用于接受用户输入.一个是 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接受来自用户的字符串,并在当前Python上下文中评估该字符串.当我输入dude作为输入时,它会发现dude绑定到值thefourtheye,因此求值结果为thefourtheye,并将其分配给input_variable.

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.

如果我输入当前python上下文中不存在的其他内容,则NameError将会失败.

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

Python 2.7的input的安全注意事项:

Security considerations with Python 2.7's input:

由于评估了任何用户类型,因此也带来了安全问题.例如,如果您已经在程序中使用import os加载了os模块,然后用户输入

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")

这将由python评估为函数调用表达式,并将执行该函数.如果您以提升的权限执行Python,则/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?

为演示这一点,让我们尝试再次执行input函数.

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

现在,当执行input("Enter your name: ")时,它将等待用户输入,并且该用户输入是有效的Python函数调用,因此也会被调用.这就是为什么我们再次看到Enter your name again:提示的原因.

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.

因此,最好使用这样的raw_input函数

So, you are better off with raw_input function, like this

input_variable = raw_input("Enter your name: ")

如果需要将结果转换为其他类型,则可以使用适当的函数转换raw_input返回的字符串.例如,要将输入读取为整数,请使用int函数,如此答案中所示.

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.

在python 3.x 中,只有一个函数可获取用户输入,该函数称为

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:未定义名称'...'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆