在教程中,我读到input和之间存在差异raw_input.我发现他们在Python 3.0中改变了这些函数的行为.什么是新行为?
为什么在python控制台解释这个
x = input()
Run Code Online (Sandbox Code Playgroud)
发送错误,但如果我把它放在file.py并运行它,它不会?
aar*_*ing 17
在python 2.x中,raw_input()返回一个字符串并input()在调用它的执行上下文中计算输入
>>> x = input()
"hello"
>>> y = input()
x + " world"
>>> y
'hello world'
Run Code Online (Sandbox Code Playgroud)
在python 3.x中,input已被废弃并且之前已知的功能raw_input是现在input.所以你必须手动调用,compile而不是eval你想要旧的功能.
python2.x python3.x
raw_input() --------------> input()
input() -------------------> eval(input())
Run Code Online (Sandbox Code Playgroud)
在3.x中,上面的会话是这样的
>>> x = eval(input())
'hello'
>>> y = eval(input())
x + ' world'
>>> y
'hello world'
>>>
Run Code Online (Sandbox Code Playgroud)
所以你可能在解释器上遇到错误,因为你没有在你的输入中加上引号.这是必要的,因为它已被评估.你得到名称错误的地方?
| 归档时间: |
|
| 查看次数: |
29884 次 |
| 最近记录: |