Concepts / Debugging Your Code

Debugging Your Code

A script executes statements sequentially, from top to bottom, with each statement completing before the next begins.

  • Programming

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.

thenthenthenx = 4store 4print(x)display 4x = 9store 9print(x)display 9
What happens next as the script runs from the first statement to the last?

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.

python
Output
6
10
nextnextnextscore = 6score becomes 66displayedscore = 10score becomes 1010displayed
How does each statement change the current value and determine the output that appears later?

What do you think happens?

What will this script display, in order?

  • 2 followed by 2
  • 2 followed by 7
  • 7 followed by 2
  • Nothing
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.

python

Store Values or Display Results

Statement kindMain effectVisible output
Assignment statementStores a value in a variableNo
print() statementDisplays a resultYes

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.

storesscorevariableScreenno assignment output10stored value
What changes when a value is assigned, and why does an assignment produce no screen output?
storesshowstotal = 12stores a valuetotalupdated valueprint(total)displays a result12screen output
Which statement changes a stored value, which statement displays a result, and how are their effects different?

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

EASY

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().
python

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

  1. Begin with the first statement and move downward one statement at a time.
  2. Describe what the current statement does.
  3. Record whether the statement changes a variable.
  4. Record whether the statement produces visible output.
  5. When a print() statement appears, use the value stored at that exact point in the sequence.
  6. 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

  1. A script executes statements from top to bottom, with each statement completing before the next begins.
  2. Assignment statements store values but produce no visible output.
  3. print() statements display results using the value available when they execute.
  4. Values stored earlier remain available to later statements until a later assignment changes the variable.
  5. 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.