什么是 NameError 错误?
what's the NameError mistake?
代码非常简单我刚开始编程 python
代码
man = input ("what's your name mister")
print("his name is "+man)
我在 运行 程序后收到的消息
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
alex
NameError: name 'alex' is not defined
在 python 2 中,"input" 获取您输入的内容,并尝试将其视为 python 表达式。因此它将您的字符串 "alex" 视为变量的名称。如果您没有名为 "alex" 的变量,则会在尝试查找它时产生错误。这是一个例子
alex = "Hello world"
x = input()
print x
如果我在输入中输入 "alex",这将打印 "Hello world." 如果您想将字符串作为输入,请使用 raw_input()。
alex = "Hello world"
x = raw_input()
print x
这将打印 "alex",而不是 "hello world."
如果您使用 python 3,"input" 的行为方式与 "raw_input" 在 python 2 中的行为完全相同。我只是 运行 您的代码在 python 3 中并且没有收到任何错误,因此您可能正在使用 2。
此外,here 是关于输入和 raw_input 的更多信息。
谢谢你们试图找出错误 我发现了错误,我正在点击 运行 然后 Python shell
相反,我点击了 运行 模块并且它起作用了,我再次抱歉
代码非常简单我刚开始编程 python
代码
man = input ("what's your name mister")
print("his name is "+man)
我在 运行 程序后收到的消息
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
alex
NameError: name 'alex' is not defined
在 python 2 中,"input" 获取您输入的内容,并尝试将其视为 python 表达式。因此它将您的字符串 "alex" 视为变量的名称。如果您没有名为 "alex" 的变量,则会在尝试查找它时产生错误。这是一个例子
alex = "Hello world"
x = input()
print x
如果我在输入中输入 "alex",这将打印 "Hello world." 如果您想将字符串作为输入,请使用 raw_input()。
alex = "Hello world"
x = raw_input()
print x
这将打印 "alex",而不是 "hello world."
如果您使用 python 3,"input" 的行为方式与 "raw_input" 在 python 2 中的行为完全相同。我只是 运行 您的代码在 python 3 中并且没有收到任何错误,因此您可能正在使用 2。
此外,here 是关于输入和 raw_input 的更多信息。
谢谢你们试图找出错误 我发现了错误,我正在点击 运行 然后 Python shell 相反,我点击了 运行 模块并且它起作用了,我再次抱歉