Python 2.x 和 3.x 中输入命令之间的差异 [英] Differences between input commands in Python 2.x and 3.x

查看:39
本文介绍了Python 2.x 和 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 to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input()).

这篇关于Python 2.x 和 3.x 中输入命令之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆