首页 > 解决方案 > python 3名称错误

问题描述

编辑:感谢您提供有用的答案!我下载了 Python 3.7.0,但你是对的,我的 Mac 运行的是 Python 2.7。我现在有作业:) 弄清楚如何让它运行 3.7。如果我有更多问题,我会回来的。谢谢!

初学者在这里。使用 Python Launcher 在 Mac 中执行时出现 NameError。在 Python 3.7.0 Shell 中进行测试时,它可以正常工作。我已经阅读了 NameError 问题的其他答案,但不明白我做错了什么。帮助表示赞赏。

使用的代码

first_name = input ("Hi, what's your first name? ")
print ("Hi," , first_name)

收到错误

Traceback (most recent call last):
  File "/Users/imperio/Documents/pythonpractice/Name.py", line 1, in <module>
   first_name = input ("Hi, what's your first name? ")
  File "<string>", line 1, in <module>
NameError: name 'Imperio' is not defined

标签: pythonpython-3.xnameerror

解决方案


这很可能是因为您没有使用 Python 3+ 执行它。

请检查输出python -V以查看您正在使用哪个版本执行代码。

在 Mac 上,您可能已经安装了这两个版本,Python 3 有时别名为python3 file.py

这是您的程序转换为有效的 Python2:

first_name = raw_input ("Hi, what's your first name? ")
print ("Hi, {}".format(first_name))

推荐阅读