Counter with a Closure
Medium · Functions
A closure is an inner function that remembers variables from the function that created it, even after that function has returned. To reassign such a variable inside the inner function, declare it with nonlocal.
Write make_counter(start, step) that returns a function. Each call of that function adds step to its own running value (which begins at start) and returns the new value. Two counters made by separate calls must not share their values.
The driver run_counters is already written: it makes two counters, "a" and "b", and calls them in the order given.