Debugging Your Code
A script executes statements sequentially, from top to bottom, with each statement completing before the next begins.
Follow the Execution Order
When a program contains several statements, Python does not execute them all at once. It runs them in order from top to bottom. One statement completes before the next statement begins. This execution order gives you a reliable way to understand what a script will do and to investigate behavior that is not what you expected.
Trace Each Statement
To trace a script, examine each statement in sequence and ask two questions: What does this statement do, and does it produce output? Also note whether it changes a variable. This separates the internal work of the program from the visible result on the screen.
Tracing a Value from Start to Finish
Determine what the script displays.
Statement 1: The assignment statement stores the value 6 in the variable score. It does not display anything.
Statement 2: The print() statement displays the current value of score, which is 6.
Statement 3: The assignment statement stores the value 10 in score. The earlier value remains available until this assignment changes the variable, and this assignment itself displays nothing.
Statement 4: The print() statement displays the current value of score, which is now 10.
The visible output is 6 followed by 10.
6
10What do you think happens?
What will this script display, in order?
Reveal answer
Answer: 2 followed by 7
The first print() runs while number has the value 2. The later assignment changes number to 7, and the second print() displays the updated value.
Store Values or Display Results
| Statement kind | Main effect | Visible output |
|---|---|---|
| Assignment statement | Stores a value in a variable | No |
| print() statement | Displays a result | Yes |
An assignment statement changes what value is stored for a variable, but the assignment does not by itself place text on the screen. A print() statement displays a result. Values assigned in earlier statements remain available for later statements, so a later print() can display a value that was stored earlier or a newer value that replaced it.
Mistakes Beginners Make
Expecting an assignment to display its value
The assignment stores 25 but does not produce visible output.
Fix:
Use print(value) when the value needs to appear on the screen.Reading the final value into every earlier print() statement
Each print() runs at its position in the sequence. The first print() runs before amount changes to 8.
Fix:
Trace the current value at each line. The output is 3 followed by 8.Skipping assignment statements while tracing
The second assignment makes a value available to the later print() statement.
Fix:
Record what each assignment stores, even when it produces no output.Looking only at statements that produce output
The final output depends on the later assignment, even though neither assignment displays anything.
Fix:
Mark both output and value changes while moving from top to bottom.
Practice the Trace
Trace this script from top to bottom. Write down the value of label after each assignment and predict the exact output.
Hints
- The first assignment stores a value but displays nothing.
- The first print() uses the value stored before the next assignment.
- The second assignment changes the value before the final print().
Practice Solution
Determine the output of the label script.
First line: label receives the value 4. Nothing is displayed.
Second line: print(label) displays 4.
Third line: label receives the value 11. Nothing is displayed.
Fourth line: print(label) displays the current value, 11.
The output is 4 followed by 11.
A Reliable Debugging Routine
- Begin with the first statement and move downward one statement at a time.
- Describe what the current statement does.
- Record whether the statement changes a variable.
- Record whether the statement produces visible output.
- When a print() statement appears, use the value stored at that exact point in the sequence.
- Compare your predicted output with the program's actual output to locate where your understanding differs.
This routine turns debugging into a trace rather than a guess. The important information is not only what a variable contains at the end, but also what it contained when each statement executed. Since statements run sequentially, an earlier print() can display an earlier value even when a later assignment changes that variable.
Key Takeaways
- A script executes statements from top to bottom, with each statement completing before the next begins.
- Assignment statements store values but produce no visible output.
- print() statements display results using the value available when they execute.
- Values stored earlier remain available to later statements until a later assignment changes the variable.
- Line-by-line tracing helps you predict output and locate unexpected behavior.
Key Takeaways
- Execution proceeds sequentially from the first statement to the last.
- Assignments change stored values without displaying them.
- print() displays the current value at its position in the script.
- Tracing every statement makes multi-statement output predictable.
- Debugging begins by comparing each statement's effect with the result you expected.