好的,所以我使用了很多输入命令,而且我知道在 Python2 中我可以这样做:
text = raw_input ('Text here')
但是现在我使用 Python 3,我想知道它们之间有什么区别:
text = input('Text here')
和:
text = eval(input('Text here'))
我什么时候必须使用其中之一?
最佳答案
在 Python 3.x 中,raw_input
变成了 input
而 Python 2.x 的 input
被移除了。因此,通过在 3.x 中执行此操作:
text = input('Text here')
你基本上是在 2.x 中这样做:
text = raw_input('Text here')
在 3.x 中这样做:
text = eval(input('Text here'))
与在 2.x 中这样做是一样的:
text = input('Text here')
以下是 Python 文档的简要总结:
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 - Python 2.x 和 3.x 中输入命令的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19036188/