Single call on a new counter
Input
create_counter()()
Output
1
create_counter() returns a new counter function. Calling it once increments the internal count from 0 to 1 and returns 1.
Full lesson preview
Learn how to use the nonlocal keyword by creating a closure that keeps internal state across calls.
Problem statement
Task
Examples
Input
create_counter()()
Output
1
create_counter() returns a new counter function. Calling it once increments the internal count from 0 to 1 and returns 1.
Input
(lambda c: (c(), c(), c()))(create_counter())
Output
(1, 2, 3)
The lambda creates one counter, calls it three times in sequence, and returns the tuple of results showing the nonlocal state incrementing.
Input format
Output format
Constraints
Samples
Input
(lambda c: (c(), c()))(create_counter())
Output
(1, 2)
Two calls on the same counter produce 1 then 2.