Python 3 中的“raw_input()”和“input()”有什么区别?

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

What's the difference between `raw_input()` and `input()` in Python 3?

pythonpython-3.xinput

提问by pkumar

What is the difference between raw_input()and input()in Python 3?

Python 3 中的raw_input()和 有什么区别input()

采纳答案by Sven Marnach

The difference is that raw_input()does not exist in Python 3.x, while input()does. Actually, the old raw_input()has been renamed to input(), and the old input()is gone, but can easily be simulated by using eval(input()). (Remember that eval()is evil. Try to use safer ways of parsing your input if possible.)

不同之处在于raw_input()Python 3.x 中不存在,而input()存在。实际上, oldraw_input()已重命名为input(),并且 oldinput()不见了,但可以很容易地使用 进行模拟eval(input())。(记住这eval()是邪恶的。如果可能,尝试使用更安全的方式来解析您的输入。)

回答by Thomas K

In Python 2, raw_input()returns a string, and input()tries to run the input as a Python expression.

在 Python 2 中raw_input()返回一个字符串,并input()尝试将输入作为 Python 表达式运行。

Since getting a string was almost always what you wanted, Python 3 does that with input(). As Sven says, if you ever want the old behaviour, eval(input())works.

由于获取字符串几乎总是您想要的,因此 Python 3 使用input(). 正如斯文所说,如果你想要旧的行为,那就行了eval(input())

回答by screenglow

Python 2:

蟒蛇2:

  • raw_input()takes exactly what the user typed and passes it back as a string.

  • input()first takes the raw_input()and then performs an eval()on it as well.

  • raw_input()获取用户输入的内容并将其作为字符串传回。

  • input()首先获取raw_input(),然后eval()也对它执行 an 。

The main difference is that input()expects a syntactically correct python statement where raw_input()does not.

主要区别在于input()期望语法正确的 python 语句,raw_input()而不是。

Python 3:

蟒蛇3:

  • raw_input()was renamed to input()so now input()returns the exact string.
  • Old input()was removed.
  • raw_input()被重命名为input()所以现在input()返回确切的字符串。
  • 旧的input()被删除了。

If you want to use the old input(), meaning you need to evaluate a user input as a python statement, you have to do it manually by using eval(input()).

如果您想使用旧的input(),这意味着您需要将用户输入评估为 python 语句,您必须使用eval(input()).

回答by Rubal

I'd like to add a little more detail to the explanation provided by everyone for the python 2 users. raw_input(), which, by now, you know that evaluates what ever data the user enters as a string. This means that python doesn't try to even understand the entered data again. All it will consider is that the entered data will be string, whether or not it is an actual string or int or anything.

我想在每个人为python 2用户提供的解释中添加更多细节。raw_input(),到目前为止,您知道它会评估用户作为字符串输入的任何数据。这意味着 python 甚至不会尝试再次理解输入的数据。它会考虑的是输入的数据将是字符串,无论它是实际的字符串还是 int 或任何东西。

While input()on the other hand tries to understand the data entered by the user. So the input like helloworldwould even show the error as 'helloworld is undefined'.

input()另一方面试图理解用户输入的数据。因此,像这样的输入helloworld甚至会将错误显示为“ helloworld is undefined”。

In conclusion, for python 2, to enter a string too you need to enter it like 'helloworld' which is the common structure used in python to use strings.

总之,对于python 2,要输入字符串,您也需要像 ' helloworld'一样输入它,这是 python 中使用字符串的常用结构。

回答by Harsha Vardhan

In Python 3, raw_input()doesn't exist which was already mentioned by Sven.

在 Python 3 中,raw_input()不存在 Sven 已经提到的。

In Python 2, the input()function evaluates your input.

在 Python 2 中,该input()函数会评估您的输入。

Example:

例子:

name = input("what is your name ?")
what is your name ?harsha

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    name = input("what is your name ?")
  File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined

In the example above, Python 2.x is trying to evaluate harsha as a variable rather than a string. To avoid that, we can use double quotes around our input like "harsha":

在上面的例子中,Python 2.x 试图将harsha 作为一个变量而不是一个字符串来计算。为了避免这种情况,我们可以在我们的输入周围使用双引号,例如“harsha”:

>>> name = input("what is your name?")
what is your name?"harsha"
>>> print(name)
harsha

raw_input()

原始输入()

The raw_input()` function doesn't evaluate, it will just read whatever you enter.

raw_input()` 函数不求值,它只会读取您输入的任何内容。

Example:

例子:

name = raw_input("what is your name ?")
what is your name ?harsha
>>> name
'harsha'

Example:

例子:

 name = eval(raw_input("what is your name?"))
what is your name?harsha

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    name = eval(raw_input("what is your name?"))
  File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined

In example above, I was just trying to evaluate the user input with the evalfunction.

在上面的示例中,我只是尝试使用该eval函数评估用户输入。

回答by Josef Klotzner

If You want to ensure, that your code is running with python2 and python3, use function input () in your script and add this to begin of your script:

如果您想确保您的代码与 python2 和 python3 一起运行,请在脚本中使用函数 input () 并将其添加到脚本的开头:

from sys import version_info
if version_info.major == 3:
    pass
elif version_info.major == 2:
    try:
        input = raw_input
    except NameError:
        pass
else:
    print ("Unknown python version - input function not safe")