在下面的代码中,
def makeAverage():
series = []
def average(newValue):
series.append(newValue)
total = sum(series)
return total/len(series)
return average
python 解释器不series
希望nonlocal
在average()
.
但是在下面的代码中
def makeAverage():
count = 0
total = 0
def average(newValue):
nonlocal count, total
count += 1
total += newValue
return total/count
return average
问题:
为什么 python 解释器期望count
&在中total
声明?nonlocal
average()