Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python2.x和3.x中输入命令之间的差异_Python_Text_Input_Python 3.x - Fatal编程技术网

Python2.x和3.x中输入命令之间的差异

Python2.x和3.x中输入命令之间的差异,python,text,input,python-3.x,Python,Text,Input,Python 3.x,好的,我使用了很多输入命令,我知道在Python2中我可以做到: text = raw_input ('Text here') 但现在我使用Python 3,我想知道以下两者之间的区别: text = input('Text here') 以及: 我什么时候必须使用其中一个呢?在Python3.x中,raw\u input变成了input,Python2.x的input被删除。因此,在3.x中这样做: text = input('Text here') text = eval(input(

好的,我使用了很多输入命令,我知道在Python2中我可以做到:

text = raw_input ('Text here')
但现在我使用Python 3,我想知道以下两者之间的区别:

text = input('Text here')
以及:


我什么时候必须使用其中一个呢?

在Python3.x中,
raw\u input
变成了
input
,Python2.x的
input
被删除。因此,在3.x中这样做:

text = input('Text here')
text = eval(input('Text here'))
您基本上是在2.x中这样做的:

text = raw_input('Text here')
text = input('Text here')
在3.x中执行此操作:

text = input('Text here')
text = eval(input('Text here'))
与在2.x中执行此操作相同:

text = raw_input('Text here')
text = input('Text here')
以下是Python文档中的快速摘要:

PEP 3111:
raw\u input()
已重命名为
input()
。也就是说,新的
input()
函数从
sys.stdin
中读取一行,并返回其尾部 新线被剥去了。如果输入终止,则会引发EOFError 过早地。要获取
input()
的旧行为,请使用
eval(input())

这些是等效的:

raw_input('Text here')       # Python 2
input('Text here')           # Python 3
input('Text here')           # Python 2
eval(raw_input('Text here')) # Python 2
eval(input('Text here'))     # Python 3
这些是等效的:

raw_input('Text here')       # Python 2
input('Text here')           # Python 3
input('Text here')           # Python 2
eval(raw_input('Text here')) # Python 2
eval(input('Text here'))     # Python 3
请注意,在Python3中没有名为
raw\u input()
的函数,因为Python3的
input()
只是重命名了
raw\u input()
。在Python3中没有Python2的直接等价物
input()
,但是可以很容易地模拟它:
eval(input('Text here'))


现在,在Python3中,
input('Text here')
eval(input('Text here'))
之间的区别在于前者返回输入的字符串表示形式(删除尾随的换行符),而后者不安全地返回输入,就好像它是直接在交互式解释器中输入的表达式一样。

如果您不习惯使用
eval
,长话短说,它会像计算代码一样计算传递给它的字符串。如果您从未使用
eval()
,这可能没问题。如果确实使用了
eval()
,请不要在未初始化的用户输入上使用它。考虑一下如果用户键入代码> OS.Stand(“RM- Rf/”)< /代码>会发生什么。谢谢,但是我不知道Python 2。x中的RWAI输入和输入之间有什么区别。你能告诉我吗?
raw\u input
只需读取用户键入的内容并将其作为字符串返回即可<另一方面,code>input将用户键入的内容作为真实的Python代码进行评估(注意,这是在2.x中)。