Home βΊ Problems βΊ Using a variable before it's defined
Using a variable before it's defined
π Fix the bug Topic: functions Difficulty: medium Free
What this problem practises
Function bugs are mostly about names and values: positional argument order, default parameters evaluated once at definition time, local versus global scope, and the implicit None returned whenever a function has no return.
This is a fix-the-bug problem: a short program is given in its broken state, and the task is to repair the code so it produces the required output. The interactive editor runs your fixed version in the browser and checks it for you β no setup, no local interpreter.
Difficulty: medium β expect to hold two interacting rules in your head at once, such as a boundary combined with a mutation.
The challenge
This should calculate and print the area of a rectangle, but it crashes. Fix it.
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The error it raises
Run the broken snippet and Python stops with this exact error: NameError: name 'total' is not defined. Match it to the Python errors guide, then repair the line that produced it β knowing the message is half the fix.
The snippet
The interactive editor pre-fills exactly this code, broken and ready to repair:
def area():
print(length * width)
length = 5
width = 3
area()
print(total)Understand the rule
This bug is about statement order: the code reads a variable on a line that runs before the assignment that creates it. Python executes a script strictly top to bottom, and a function body runs not where it is written but where it is called β so an assignment placed later in the file has simply not happened yet when the earlier line asks for the value.
How to trace it
- Number the lines in execution order, which for straight-line code is top to bottom.
- Mark the line where the variable is first assigned and the line where it is first read.
- If the read comes first, move the assignment above it β or move the reading code into a function called later.
- Re-run and confirm the same name is not also shadowed inside a function scope.
Common wrong fixes
- Declaring the variable at the bottom of the file and expecting Python to hoist it like JavaScript's var.
- Fixing the crash by duplicating the assignment in both places, leaving two sources of truth.
- Blaming the function definition order when the real issue is the order of the calls.
Rule to remember
Assignment must execute before use. Python has no hoisting: the position of a line in the file matters only through when it actually runs.
π’ Learning path: Variables and Data Types
Problem 2 of 13 in this path. Work them in order β each one builds on the rule before it.
More functions problems
Keep going
- Python debugging guide β a systematic method for finding bugs like this one fast.
- Browse every functions problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.