为什么Python 3中的`input`抛出NameError:name ...未定义 [英] Why is `input` in Python 3 throwing NameError: name... is not defined
本文介绍了为什么Python 3中的`input`抛出NameError:name ...未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串变量test
,在Python 2.x中可以正常工作.
I have a string variable test
, in Python 2.x this works fine.
test = raw_input("enter the test")
print test
但是在Python 3.x中,我这样做:
But in Python 3.x, I do:
test = input("enter the test")
print test
输入字符串sdas
,我收到一条错误消息
with the input string sdas
, and I get an error message
Traceback (most recent call last):
File "/home/ananiev/PycharmProjects/PigLatin/main.py", line 5, in <module>
test = input("enter the test")
File "<string>", line 1, in <module>
NameError: name 'sdas' is not defined
推荐答案
您正在使用Python 2解释器运行Python 3代码.如果不是这样,您的print
语句将抛出 SyntaxError
提示您输入之前.
You're running your Python 3 code with a Python 2 interpreter. If you weren't, your print
statement would throw up a SyntaxError
before it ever prompted you for input.
结果是您正在使用Python 2的 input
尝试eval
您的输入(大概是sdas
),发现它是无效的Python,然后死亡.
The result is that you're using Python 2's input
, which tries to eval
your input (presumably sdas
), finds that it's invalid Python, and dies.
这篇关于为什么Python 3中的`input`抛出NameError:name ...未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文