问题:Python 3中的raw_input()和input()有什么区别?

raw_input()input()Python 3有什么区别?

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


回答 0

区别在于raw_input()Python 3.x中不存在,而input()确实存在。实际上,raw_input()已将旧名称重命名为input(),而旧名称input()已消失,但是可以使用轻松地对其进行模拟eval(input())。(请记住这eval()是邪恶的。如果可能,请尝试使用更安全的方法来解析输入。)

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


回答 1

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

由于获取字符串几乎总是您想要的,因此Python 3做到了input()。正如Sven所说,如果您想要旧的行为,那就eval(input())可以了。

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

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.


回答 2

Python 2:

  • raw_input() 完全接受用户键入的内容,并将其作为字符串传递回。

  • input()首先采用raw_input(),然后对其执行eval()

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

Python 3:

  • raw_input()被重命名为,input()因此现在input()返回确切的字符串。
  • 旧的input()被删除。

如果要使用旧的input()(意味着需要将用户输入评估为python语句),则必须使用手动进行操作eval(input())

Python 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.

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

Python 3:

  • raw_input() was renamed to input() so now input() returns the exact string.
  • Old input() was removed.

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()).


回答 3

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

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

例:

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

在上面的示例中,Python 2.x尝试将rahda评估为变量而非字符串。为了避免这种情况,我们可以在输入中使用双引号,例如“ harsha”:

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

raw_input()

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

例:

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

例:

 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

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

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

In Python 2, the input() function evaluates your 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”:

>>> 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.

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 eval function.


回答 4

我想在每个人为python 2用户提供的解释中添加更多细节。raw_input(),到现在为止,您已经知道该功能可以评估用户以字符串形式输入的数据。这意味着python甚至不会尝试再次理解输入的数据。它只会考虑输入的数据将是字符串,无论它是实际的字符串还是int或其他任何值。

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

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

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.

While input() on the other hand tries to understand the data entered by the user. So the input like helloworld would even show the error as ‘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.


回答 5

如果您想确保自己的代码与python2和python3一起运行,请在脚本中使用function 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")

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:

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。