什么';Python3中的'raw_input()'和'input()'之间的区别是什么?

什么';Python3中的'raw_input()'和'input()'之间的区别是什么?,python,python-3.x,input,Python,Python 3.x,Input,Python 3中的raw\u input()和input()之间有什么区别?区别在于raw\u input()在Python 3.x中不存在,而input()存在。实际上,旧的raw_input()已重命名为input(),旧的input()已不存在,但可以使用eval(input())轻松模拟。(请记住,eval()是邪恶的。如果可能,请尝试使用更安全的方法解析输入。)在Python2中,raw\u input()返回字符串,并且input()尝试将输入作为Python表达式运行 因为获取字

Python 3中的
raw\u input()
input()
之间有什么区别?

区别在于
raw\u input()
在Python 3.x中不存在,而
input()
存在。实际上,旧的
raw_input()
已重命名为
input()
,旧的
input()
已不存在,但可以使用
eval(input())
轻松模拟。(请记住,
eval()
是邪恶的。如果可能,请尝试使用更安全的方法解析输入。)

在Python2中,
raw\u input()
返回字符串,并且
input()
尝试将输入作为Python表达式运行

因为获取字符串几乎总是您想要的,所以Python3通过
input()
来实现这一点。正如斯文所说,如果您想要旧的行为,
eval(input())
可以工作。

Python2:
  • raw\u input()

  • input()
    首先获取
    原始输入()
    ,然后对其执行
    eval()

主要区别在于
input()
需要语法正确的python语句,而
raw\u input()
不需要

Python 3:
  • raw\u input()
    被重命名为
    input()
    ,因此现在
    input()
    返回准确的字符串
  • 旧的
    input()
    已被删除

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

,手动执行此操作。我想在大家为python 2用户提供的解释中添加一点更详细的内容
raw_input()
,现在,您已经知道,它会计算用户以字符串形式输入的任何数据。这意味着python甚至不再试图理解输入的数据。它将考虑输入的数据将是字符串,无论它是实际的字符串还是int或任何东西。 另一方面,
input()
试图理解用户输入的数据。因此像
helloworld
这样的输入甚至会将错误显示为“
helloworld未定义”


总之,对于Python2,要输入字符串也需要像“
helloworld
”这样输入,这是python中使用字符串的常用结构

在Python3中,
raw\u 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
 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
原始输入()

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

示例:

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

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
 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
name=eval(原始输入(“你叫什么名字?”))
你叫什么名字?哈莎
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
name=eval(原始输入(“你叫什么名字?”))
文件“”,第1行,在
NameError:未定义名称“harsha”

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

如果您想确保代码使用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")

您应该补充一点,Python 3没有
raw_input()
。将输入作为Python表达式运行有什么用?@akshayvijayn,可能是为了输入数字。但这是完全不安全的。“原始输入<代码>和原始输入<代码>之间有什么区别?”——“区别在于没有原始输入<代码>和原始输入<代码>”…我认为这是一个很大的区别!在Python2中,我猜他们假设程序员希望实际“执行”用户输入的命令,因为最初(我猜)请求用户输入可能只是为了这个目的。但当他们意识到程序员可能也想获得“原始”输入时,他们设计了另一个称为“原始”输入的函数。在Python3中,他们注意到了这一点,简单地删除了原始输入默认值的执行,只生成了一个简单的函数。Repl.it,运行Py3.5.1时将raw_input()作为关键字。需要指出的是,
eval
(和
exec
)通常应该避免,因为它们可能存在安全风险。有关详细信息,请参见SO资深内德·巴奇尔德。当然,这个建议也适用于旧的Python2
input
@PM2Ring,我在答案中添加了一个警告。当然,对于
eval()
exec()
,都有有效的用例,但是您首先需要理解为什么不应该使用
eval()
在决定使用它之前。如何使输入Python 2和Python 3兼容的程序?要做到这一点,请尝试将
input
设置为
raw\u input
并忽略名称错误。查找“六”库以了解Python 2和Python 3的兼容性。