A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-70.py
1n=5
2deff():
3n=n+1
4returnn
5try:
6print(f())
7exceptUnboundLocalErrorase:
8print(type(e).__name__)
Scope
Answer & explanation
Console output
UnboundLocalError
Why
The assignment n = n + 1 makes n a local for the whole of f, so the n on the right-hand side refers to that local, not the global 5. Since the local has no value yet when the expression is evaluated, Python raises UnboundLocalError. Declaring global n would let it read and update the module-level n.