← Quiz: Scope and Closures: all 10 questions
Quiz: Scope and Closures · Question 3 of 10
What does this print? (code)
What does this print?
def make():
n = 0
def step():
nonlocal n
n += 5
return n
return step
s = make()
s()
print(s())
- A.5
- B.0
- C.10
- D.An error
Answer & explanation
Correct answer: 10
The closure keeps n alive between calls: 5 after the first call, 10 after the second.
Can you get the rest right without peeking?
Take the timed Quiz: Scope and Closures quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →