In the below code,
def makeAverage():
series = []
def average(newValue):
series.append(newValue)
total = sum(series)
return total/len(series)
return average
python interpreter does not expect series
to be nonlocal
in average()
.
But in the below code
def makeAverage():
count = 0
total = 0
def average(newValue):
nonlocal count, total
count += 1
total += newValue
return total/count
return average
Question:
Why python interpreter expects count
& total
declared nonlocal
in average()
?