Python3名称错误
编辑:感谢您提供的有用答案!我下载了Python3.7.0,但你是对的,我的Mac电脑运行的是Python2.7。我现在有作业:)想办法让它运行3.7。如果我有更多的问题,我会回来的。谢谢大家! 我是初学者。使用Python启动器在Mac中执行时出现NameError。在Python3.7.0shell中进行测试时,它工作正常。我读过其他关于NameError问题的答案,但不明白我做错了什么。谢谢你的帮助 使用的代码Python3名称错误,python,python-3.x,nameerror,Python,Python 3.x,Nameerror,编辑:感谢您提供的有用答案!我下载了Python3.7.0,但你是对的,我的Mac电脑运行的是Python2.7。我现在有作业:)想办法让它运行3.7。如果我有更多的问题,我会回来的。谢谢大家! 我是初学者。使用Python启动器在Mac中执行时出现NameError。在Python3.7.0shell中进行测试时,它工作正常。我读过其他关于NameError问题的答案,但不明白我做错了什么。谢谢你的帮助 使用的代码 first_name = input ("Hi, what's your fi
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
收到错误
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
回溯(最近一次呼叫最后一次):
文件“/Users/imperio/Documents/pythonpracce/Name.py”,第1行,在
first\u name=input(“嗨,你的名字是什么?”)
文件“”,第1行,在
NameError:未定义名称“Imperio”
这很可能是因为您没有使用Python 3+执行它
请检查python-V
的输出,以查看执行代码的版本
在mac上,您可能已经安装了这两个版本,Python 3有时会在python3 file.py下使用别名
以下是您转换为有效Python 2的程序:
first_name = raw_input ("Hi, what's your first name? ")
print ("Hi, {}".format(first_name))
这很可能是因为您没有使用Python3+执行它
请检查python-V
的输出,以查看执行代码的版本
在mac上,您可能已经安装了这两个版本,Python 3有时会在python3 file.py下使用别名
以下是您转换为有效Python 2的程序:
first_name = raw_input ("Hi, what's your first name? ")
print ("Hi, {}".format(first_name))
您正在运行Python 2。在Python 2中,input
执行输入<代码>原始输入
仅返回输入的字符串
下面是一个例子:
>>> x = 1
>>> y = 2
>>> z = 3
>>> print input('variable? ')
variable? x # Note output is the value of the variable
1
>>> print input('variable? ')
variable? w # Variable 'w' doesn't exist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'w' is not defined
>>> print raw_input('variable? ') # Raw input just returns the input.
variable? x
x
>x=1
>>>y=2
>>>z=3
>>>打印输入(“变量”)
变量x#注意输出是变量的值
1.
>>>打印输入(“变量”)
变量w#变量“w”不存在
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第1行,在
NameError:未定义名称“w”
>>>打印原始输入(“变量”)#原始输入只返回输入。
变量x
x
您正在运行Python 2。在Python 2中,input
执行输入<代码>原始输入
仅返回输入的字符串
下面是一个例子:
>>> x = 1
>>> y = 2
>>> z = 3
>>> print input('variable? ')
variable? x # Note output is the value of the variable
1
>>> print input('variable? ')
variable? w # Variable 'w' doesn't exist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'w' is not defined
>>> print raw_input('variable? ') # Raw input just returns the input.
variable? x
x
>x=1
>>>y=2
>>>z=3
>>>打印输入(“变量”)
变量x#注意输出是变量的值
1.
>>>打印输入(“变量”)
变量w#变量“w”不存在
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第1行,在
NameError:未定义名称“w”
>>>打印原始输入(“变量”)#原始输入只返回输入。
变量x
x
这不是Python 3.7。您使用的是Python 2。谢谢。我下载了Python3.7。来自python.org。我使用的是一本基于Python3.1的Python初学者书籍,可能与Python3.7有所不同。您使用的是Python 2。谢谢。我下载了Python3.7。来自python.org。我正在使用一本基于Python 3.1的Python初学者手册,该手册可能与