python - 为什么 Python 3 中的 `input` 抛出 NameError : name. .. 未定义

标签 python python-3.x string input eval

<分区>

我有一个字符串变量 test,在 Python 2.x 中它工作正常。

test = raw_input("enter the test") 
print test

但在 Python 3.x 中,我这样做:

test = input("enter the test") 
print test

输入字符串sdas,我得到一条错误信息

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。在它提示您输入之前。

结果是您使用的是 Python 2 的 input ,它会尝试 eval 您的输入(大概是 sdas),发现它是无效的 Python,然后死掉。

关于python - 为什么 Python 3 中的 `input` 抛出 NameError : name. .. 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16625669/

相关文章:

python - 有人可以在 python (scipy.io) 中解释 mdict,例如在 scipy.io.savemat() 中吗?

Python 在程序设置中安装 pip 模块?

c++ - 从 python 解释器调用方法

python-3.x - 我的格式有问题。(Python)

python - 如何将参数从 Python 3.x 中的类定义传递给元类?

python - 尝试创建函数以提取特殊字符之间的切片

python - Mako 模板 : How to find the name of the template which the current template is included by?

python - 动态更改函数 __name__ 会抛出 AttributeError : 'method' object has no attribute '__name__'

c++ - 如何包含字符串标题?

javascript - 我可以使用 jquery 字符串来定位 id 吗?