我有一个简单的代码像这样的:蟒蛇变量
def function():
ciao = stuff + stuff2
return ciao
def megaf():
stuff = 1
stuff2 = 3
for t in range(10):
stuff += 1
stuff2 += 2
print function()
megaf()
当我运行的代码是
NameError: global name 'stuff' is not defined
我得到的错误,我不明白为什么,因为我确定了变量。
我试着定义了两个变量stuff
和stuff2
以外的函数,但我得到的错误“UnboundLocalError:本地变量'的东西引用前分配”。 我实际上无法理解问题是什么,所以我甚至无法找出解决方案。
你必须将'stuff'和'stuff2'传入'function'。 –
变量有一个叫做**范围**的东西。这需要深入的解释;例如,尝试阅读[此链接](http://python-textbok.readthedocs.org/en/latest/Variables_and_Scope.html)。 – Claudiu