Python `input` 和 `raw_input` 之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3800846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:48:34  来源:igfitidea点击:

Differences between `input` and `raw_input`

python

提问by Guillermo Siliceo Trueba

In a tutorial, I read that that there is a difference between inputand raw_input. I discovered that they changed the behavior of these functions in the Python 3.0. What is the new behavior?

在教程中,我读到input和之间存在差异raw_input。我发现他们在 Python 3.0 中改变了这些函数的行为。什么是新行为?

And why in the python console interpreter this

为什么在 python 控制台解释器中这个

x = input()

Sends an error but if I put it in a file.py and run it, it does not?

发送错误,但如果我将它放在 file.py 中并运行它,它不会?

采纳答案by aaronasterling

In python 2.x, raw_input()returns a string and input()evaluates the input in the execution context in which it is called

在 python 2.x 中,raw_input()返回一个字符串并input()在调用它的执行上下文中评估输入

>>> x = input()
"hello"
>>> y = input()
x + " world"
>>> y
'hello world'

In python 3.x, inputhas been scrapped and the function previously known as raw_inputis now input. So you have to manually call compileand than evalif you want the old functionality.

在 python 3.x 中,input已被废弃,以前称为raw_input现在的函数input。因此compileeval如果您想要旧功能,则必须手动调用。

python2.x                    python3.x

raw_input()   --------------> input()               
input()  -------------------> eval(input())     

In 3.x, the above session goes like this

在 3.x 中,上面的 session 是这样的

>>> x = eval(input())
'hello'
>>> y = eval(input())
x + ' world'
>>> y
'hello world'
>>> 

So you were probably getting an error at the interpretor because you weren't putting quotes around your input. This is necessary because it's evaluated. Where you getting a name error?

因此,您可能在解释器中遇到错误,因为您没有在输入周围加上引号。这是必要的,因为它被评估。您在哪里收到名称错误?

回答by Blueice

input() vs raw_input()

input() 与 raw_input()

raw_input collects the characters the user types and presents them as a string. input() doesn't just evaluate numbers but rather treats any input as Python code and tries to execute it. Knowledgeable but malicious user could type in a Python command that can even deleted a file. Stick to raw_input() and convert the string into the data type you need using Python's built in conversion functions.

raw_input 收集用户键入的字符并将它们显示为字符串。input() 不只是计算数字,而是将任何输入视为 Python 代码并尝试执行它。知识渊博但恶意的用户可以输入甚至可以删除文件的 Python 命令。坚持使用 raw_input() 并使用 Python 的内置转换函数将字符串转换为您需要的数据类型。

Also input(), is not safe from user errors! It expects a valid Python expression as input; if the input is not syntactically valid, a SyntaxError will be raised.

还有 input(),对于用户错误是不安全的!它需要一个有效的 Python 表达式作为输入;如果输入在语法上无效,则会引发 SyntaxError。

回答by aniketectech

Its simple:

这很简单:

  1. raw_input()returns string values
  2. while input()return integer values
  1. raw_input()返回字符串值
  2. whileinput()返回整数值

For Example:

例如:

1.

1.

x = raw_input("Enter some value = ")
print x

Output:

输出:

Enter some value = 123
'123'

2.

2.

y = input("Enter some value = ") 
print y

Output:

输出:

Enter some value = 123
123

Hence if we perform x + x =It will output as 123123

因此,如果我们执行x + x =它将输出为 123123

while if we perform y + y =It will output as 246

而如果我们执行y + y =它会输出为 246