Python 2.x和3.x中输入命令之间的差异 [英] Differences between input commands in Python 2.x and 3.x
问题描述
好,所以我使用了很多输入命令,并且我了解在Python2中我可以做到:
Ok, so i use a lot of input commands, and I understood that in Python2 I can do:
text = raw_input ('Text here')
但是现在我使用Python 3,我想知道两者之间有什么区别:
But now that i use Python 3 I was wondering what's the difference between:
text = input('Text here')
和:
text = eval(input('Text here'))
我何时必须使用一个或另一个?
when do I have to use the one or the other?
推荐答案
在Python 3.x中,raw_input
变为input
,并删除了Python 2.x的input
.因此,通过在3.x中执行此操作:
In Python 3.x, raw_input
became input
and Python 2.x's input
was removed. So, by doing this in 3.x:
text = input('Text here')
您基本上是在2.x中执行此操作:
you are basically doing this in 2.x:
text = raw_input('Text here')
在3.x中执行此操作:
Doing this in 3.x:
text = eval(input('Text here'))
与在2.x中执行此操作相同:
is the same as doing this in 2.x:
text = input('Text here')
以下是Python文档的快速摘要:
Here is a quick summary from the Python Docs:
PEP 3111:
raw_input()
重命名为input()
.也就是说,新的input()
函数从sys.stdin
读取一行,并在结尾处返回 换行符已删除.如果输入终止,则它升高EOFError
过早地.要获取input()
的旧行为,请使用eval(input())
.
PEP 3111:
raw_input()
was renamed toinput()
. That is, the newinput()
function reads a line fromsys.stdin
and returns it with the trailing newline stripped. It raisesEOFError
if the input is terminated prematurely. To get the old behavior ofinput()
, useeval(input())
.
这篇关于Python 2.x和3.x中输入命令之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!