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屋!