在python 2.x中,
raw_input()返回一个字符串并
input()在调用它的执行上下文中评估输入
>>> x = input()"hello">>> y = input()x + " world">>> y'hello world'
在python 3.x中,
input已被废弃,该函数以前称为
raw_inputnow
input。因此,您必须手动调用,
compile而不是
eval想要旧功能。
python2.x python3.xraw_input() --------------> input() input() -------------------> eval(input())
在3.x中,以上会话如下所示
>>> x = eval(input())'hello'>>> y = eval(input())x + ' world'>>> y'hello world'>>>
因此,您可能在解释器中遇到了错误,因为您没有在输入中加上引号。这是必需的,因为已对其进行了评估。您在哪里收到名称错误?