i具有以下循环结构以及问题,因此由于 UnboundLocalError
:
while True:
def function_1():
def function_2():
x += 1
print(x)
function_2()
function_1()
我的解决方案现在是:
x = 0
while True:
def function_1():
def function_2():
global x
x += 1
print(x)
function_2()
function_1()
是否有其他解决方案,没有global
?
使用可变值。
x = []
x.append(0)
while True:
def function_1():
def function_2():
x[0]= x[0]+1
print x[0]
function_2()
function_1()
通过并返回x到所有功能。
x = 0
while True:
def function_1(x1):
def function_2(x2):
x2 += 1
print(x2)
return x2
return function_2(x1)
x = function_1(x)