def make_averager():
series = [] # mutable free variable
def averager(new_value):
series.append(new_value)
total = sum(series)
return total / len(series)
return averager
>>> avg = make_averager()
>>> avg(10)
10.0
>>> avg(11)
10.5
>>> avg(15)
12.0
def make_averager():
count = 0
total = 0
def averager(new_value):
nonlocal count, total
count += 1
total += new_value
return total / count
return averager
>>> avg = make_averager()
>>> avg(10)
Traceback (most recent call last):
...
UnboundLocalError: local variable 'count' referenced before assignment
>>>
def make_averager():
count = 0
total = 0
def averager(new_value):
nonlocal count, total # Fix the problem above
count += 1
total += new_value
return total / count
return averager