当我在函数中定义变量名时,为什么会出现 NameError?
Why am I getting a NameError when I've defined my variable name in a function?
我正在尝试根据输入语句和我定义的函数构建 if/elif/else 语句。当我 运行 代码、输入语句和函数定义并调用 运行 时很好,但随后我进入我的 if/elif/else 语句并且我得到一个 NameError,告诉我一个变量我在 if/elif/else 语句中调用未定义(即使我认为我在函数中定义了它)。
对于上下文,我是 Python 的新手,我正在 Atom 中构建代码并 运行在命令提示符中将其编译。
我的输入、函数定义和 if 语句代码如下所示:
h, m, s, t = input('Rate each of the following Emotions and hit enter:\nHappiness (1-7):'), input('Meaningfulness (1-7):'), input('Stressfulness (1-7):'), input('Tiredness (1-7):')
h_int=int(h)
m_int=int(m)
s_int=int(s)
t_int=int(t)
def na():
add_h_m = h_int + m_int
div_h_m = add_h_m/2
add_s_t = s_int + t_int
div_s_t = add_s_t/2
net_affect = div_h_m - div_s_t
print(net_affect)
na()
if net_affect >= 3:
print("Doing Well")
elif net_affect <3 and net_affect >0:
print("Doing Okay")
else:
print("Not Good")
这是命令提示符输出:
C:\Users\mrkev\Desktop\PAI 724>python ex2.py
Rate each of the following Emotions and hit enter:
Happiness (1-7):6
Meaningfulness (1-7):7
Stressfulness (1-7):4
Tiredness (1-7):3
3.0
Traceback (most recent call last):
File "C:\Users\mrkev\Desktop\PAI 724\ex2.py", line 16, in <module>
if net_affect >= 3:
NameError: name 'net_affect' is not defined
我哪里错了?我认为通过在函数中定义 net_affect,我可以将它用作程序中的变量。
感谢您的帮助!
在您的文档或教程中查找 'variable scope'。
net_affect
变量是 na
函数的局部变量。
你不能从外面访问它,它在函数 returns.
时被删除
您也可以:
在函数外定义变量,说明你是在函数内使用
net_affect = None
def na():
...
global net_affect
net_affect = div_h_m - div_s_t
return 函数的值并捕获 returned 值
def na():
...
net_affect = div_h_m - div_s_t
...
return net_affect # return the value
...
net_affect_new = na() # catch the value
注意:net_affect_new
可以被称为net_affect
但它们不是同一个变量,它们在两个不同的范围内。
我正在尝试根据输入语句和我定义的函数构建 if/elif/else 语句。当我 运行 代码、输入语句和函数定义并调用 运行 时很好,但随后我进入我的 if/elif/else 语句并且我得到一个 NameError,告诉我一个变量我在 if/elif/else 语句中调用未定义(即使我认为我在函数中定义了它)。
对于上下文,我是 Python 的新手,我正在 Atom 中构建代码并 运行在命令提示符中将其编译。
我的输入、函数定义和 if 语句代码如下所示:
h, m, s, t = input('Rate each of the following Emotions and hit enter:\nHappiness (1-7):'), input('Meaningfulness (1-7):'), input('Stressfulness (1-7):'), input('Tiredness (1-7):')
h_int=int(h)
m_int=int(m)
s_int=int(s)
t_int=int(t)
def na():
add_h_m = h_int + m_int
div_h_m = add_h_m/2
add_s_t = s_int + t_int
div_s_t = add_s_t/2
net_affect = div_h_m - div_s_t
print(net_affect)
na()
if net_affect >= 3:
print("Doing Well")
elif net_affect <3 and net_affect >0:
print("Doing Okay")
else:
print("Not Good")
这是命令提示符输出:
C:\Users\mrkev\Desktop\PAI 724>python ex2.py
Rate each of the following Emotions and hit enter:
Happiness (1-7):6
Meaningfulness (1-7):7
Stressfulness (1-7):4
Tiredness (1-7):3
3.0
Traceback (most recent call last):
File "C:\Users\mrkev\Desktop\PAI 724\ex2.py", line 16, in <module>
if net_affect >= 3:
NameError: name 'net_affect' is not defined
我哪里错了?我认为通过在函数中定义 net_affect,我可以将它用作程序中的变量。
感谢您的帮助!
在您的文档或教程中查找 'variable scope'。
net_affect
变量是 na
函数的局部变量。
你不能从外面访问它,它在函数 returns.
您也可以:
在函数外定义变量,说明你是在函数内使用
net_affect = None def na(): ... global net_affect net_affect = div_h_m - div_s_t
return 函数的值并捕获 returned 值
def na(): ... net_affect = div_h_m - div_s_t ... return net_affect # return the value ... net_affect_new = na() # catch the value
注意:
net_affect_new
可以被称为net_affect
但它们不是同一个变量,它们在两个不同的范围内。