A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-85.py
1x=10
2deff():
3print(x)
4x=20
5try:
6f()
7exceptUnboundLocalErrorase:
8print(type(e).__name__)
Scope
Answer & explanation
Console output
UnboundLocalError
Why
Python decides a variable's scope for the entire function body at compile time: because x is assigned somewhere in f, x is local throughout f, shadowing the global. The print(x) then tries to read that local before the assignment line runs, raising UnboundLocalError. Adding global x would make it read and write the module-level x instead.