Objective
Create a Python program that uses a closure with nonlocal to have several counters that do not get mixed up.
Instructions
- Define
def create_counter(), which has a variablecount = 0and an inner functionnext_value()that, withnonlocal, adds 1 tocountand returns it.create_counterreturnsnext_value. - Create two independent counters:
aandb. - Read a line with letters
aorbseparated by spaces. For each letter, call the corresponding counter. - Show on one line the values obtained, in the same order and separated by spaces.
a a b a b
1 2 1 3 2
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise