Python:NameError:未定义名称“u" [英] Python: NameError: name 'u' is not defined
问题描述
我正在完成一个带有函数的简单类分配.我们必须找出哪个月份m
函数contract_v
会比contract_u
更有优势.这是我写的代码:
i am completing a simple class asignment with functions. We have to find for which month m
the functioncontract_v
will be more advantageous than the contract_u
.
This is the code I wrote:
def contract_u(m):
u=1000
for i in range (m):
u=u+80
return u
def contract_v(m):
v=1000
for i in range (m):
v=v*1.05
return v
m=1
if u>v:
m=m+1
else:
print(m)
然而,计算机是这样说的:
However, the computer says this:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-20-8909d368129a> in <module>()
12
13 m=1
---> 14 if u>v:
15 m=m+1
16 else:
NameError: name 'u' is not defined
我不明白我必须修改什么以及为什么代码不能正常运行.如果你知道哪里做错了,请指出.提前致谢.
I do not understand what I have to modify and why the code is not functionning properly. If you do know what has been done wrong, please point that out. Thank you in advance.
推荐答案
你的变量在你的函数中本地定义,所以它们不存在于它们之外,所以你应该在你的 if 语句之前添加这些行:
your variables are defined locally in your functions, so they do not exist outside of them, so you should add these lines before your if statement :
u=contract_u(m)
v=contract_v(m)
这篇关于Python:NameError:未定义名称“u"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!