Finding Bugs with the Debugger

When a program gives a wrong answer with no error to read, VS Code's debugger pauses it at a breakpoint and runs it one line at a time with Step Over and Step Into, while the Variables panel and Watch show every value change, until the line that goes wrong shows itself.

Apprentice · Python · Updated

By

After this lesson you can

  • Set a breakpoint and start VS Code's debugger, so a Python program pauses before that line runs
  • Step through a program with Step Over and Step Into, and watch its variables change to find a bug that gives a wrong answer without an error

Key takeaways

  • A breakpoint, the red dot beside a line number (F9), pauses the program before that line runs. Run, Start Debugging (F5) starts the program under the debugger.
  • Step Over (F10) runs the highlighted line and pauses on the next one. Step Into (F11) goes inside a function the line calls. Continue (F5) runs on to the next pause.
  • While the program is paused, the Variables panel shows every variable's value right now, and Watch shows the answer to any expression you add.
  • When a value surprises you, the line that ran last is where the bug is, or where to look next.

A traceback names the error and the line. The harder bugs give you nothing: the program runs to the end and prints a number, and the number is wrong. Reading the code over and over is a slow way to find one of those. Instead, pause the program partway and look at every value, the way you'd pause a game replay to see who really touched the ball first. VS Code's debugger does that.

The example is Riverside Parking Garage's end-of-day total: $4.00 an hour, never more than $24.00 a day.

A wrong answer with no error

Three drivers parked today, for 2, 1.5 and 9 hours. Make a file named day_total.py and copy this into it:

HOURLY_RATE = 4.00
DAILY_MAXIMUM = 24.00


def parking_fee(hours):
    fee = hours * HOURLY_RATE
    if fee > DAILY_MAXIMUM:
        fee = DAILY_MAXIMUM
    return fee


def day_total(hours_list):
    total = 0
    for hours in hours_list:
        total = parking_fee(hours)
    return total


print(day_total([2, 1.5, 9]))
24.0

By hand, the fees are $8.00, $6.00 and $24.00 (9 hours is $36.00, over the daily maximum), so the total should be 38.0. Nothing crashed, so there's no traceback. Pretend you can't spot the bug, and let the debugger find it.

You need VS Code with Microsoft's Python extension, which includes the Python Debugger.

Set a breakpoint and start the debugger

A breakpoint is a mark that says "pause before this line runs". Click in the margin to the left of line 15's number (total = parking_fee(hours)), and a red dot appears. Click it again to remove it, or put the cursor on a line and press F9.

Now choose Run > Start Debugging (F5). The first time, VS Code asks which debugger to use: choose Python Debugger, then Python File. The program runs until it reaches the breakpoint and pauses, with line 15 highlighted. The highlighted line hasn't run yet. The Run and Debug view opens on the left, and a small toolbar appears at the top.

VS Code paused at a breakpoint in day_total.py: a red dot beside line 15, total = parking_fee(hours), the line highlighted, the debug toolbar at the top, and the Variables panel on the left showing hours = 2 and total = 0

When a program does stop with a traceback, the line it names is a good place for a breakpoint: the program pauses right before that line fails, and you can see the values that are about to break it.

Step through it, one line at a time

The toolbar runs the program in small steps:

Key Button What it does
F10 Step Over Run the highlighted line, then pause on the next one
F11 Step Into Go inside the function the highlighted line calls, and pause on its first line
Shift+F11 Step Out Finish the function you're in, and pause back where it was called
F5 Continue Run on to the next breakpoint, or to the end
Shift+F5 Stop Stop the program

On a Mac and on many laptops, hold Fn to press the F keys.

Step Over runs a whole line, a function call included, as one step. Step Into is for when you want to see inside that function: press F11 on line 15 and the highlight jumps into parking_fee, where you can watch fee become 36.0 and then 24.0 on the day's third driver. Step Out takes you back.

Watch the values change

The Variables panel, at the top of the Run and Debug view, shows every variable that exists right now and its value. You can also hover over a variable in the editor to see its value. Watch, below it, shows the answer to any expression you give it: click + beside Watch and type total, or a whole expression such as total + parking_fee(hours). Watch works it out again at every pause.

Now hunt. Paused on line 15 for the first time, hours is 2 and total is 0. Press F10: total becomes 8.0. Press F5 to run on to the next pause: hours is 1.5. Press F10 again and watch total:

VS Code's Run and Debug view while day_total.py is paused, right after Step Over ran line 15 for the second driver: the Variables panel shows hours = 1.5 and total = 6.0, and Watch shows total = 6.0, smaller than the 8.0 it held before

total went from 8.0 down to 6.0. A running total can't get smaller, so the line that ran last is the bug: total = parking_fee(hours) replaces the total with one fee instead of adding to it. Press Shift+F5 to stop, change line 15, and run the program again:

HOURLY_RATE = 4.00
DAILY_MAXIMUM = 24.00


def parking_fee(hours):
    fee = hours * HOURLY_RATE
    if fee > DAILY_MAXIMUM:
        fee = DAILY_MAXIMUM
    return fee


def day_total(hours_list):
    total = 0
    for hours in hours_list:
        total = total + parking_fee(hours)
    return total


print(day_total([2, 1.5, 9]))
38.0

Click the red dot to remove the breakpoint when you're done. That's the habit for any quiet bug: pause right before where the answer goes wrong, then step and watch until a value surprises you. The line that ran last is where to look.

Check yourself

2 questions to try on the lesson page.

So what

When a program is wrong without an error, don't guess: pause it, look at the values, and step until one of them surprises you, because that's where the bug is.

Key terms

Related