(5) Debugging

Due: Tuesday, January 20, 11:59 PM Eastern

Don’t get stressed about bugs (mistakes) in your code. It’s ok! It happens to everyone who programs, no matter how much experience they have. Finding and fixing bugs is a skill you can learn. The idea is to gradually narrow down where the bug in your code is, so that you know what to fix.

In this tutorial, we’ll learn how to use a special debugger tool. Then we’ll consider some debugging tips. If you invest a little time pondering this information, you will save a lot of time in the long run!

What Is a Debugger?

You may recall that a bug in a program is a problem or mistake. Most commonly, the word doesn’t refer to syntax errors, but rather to semantic errors. The program runs, but then crashes or produces other wrong behavior.

A debugger is a program meant to help the programmer fix bugs. It takes just a few minutes to learn how to use a debugger. Invest this time now – you’ll be glad you did!

We’ll be using the debugger that is a part of VS Code.

Using a Debugger

Inserting a Breakpoint

Open your EECS_183 folder, the practice_code subfolder. (Or make one if you don’t already have it.) Open VS Code in that folder. Make a new .py file (I called mine debug_test.py) and paste the following into it:

x = 5
y = x + 2
x = x + 1
print(x)
print(y)

Your code should then look like this:

Code pasted into VS Code

Let’s add a breakpoint to this program. A breakpoint is not part of the code itself; rather, it’s a signal to the debugger that we want the code execution to stop at that line, so that we can inspect what’s happening. We’ll see how this works further below.

Notice that line numbers are indicated (1 - 6 here). Hover your mouse pointer just to the left of line 3 and you’ll see this:

About to add a breakpoint

Click at that point, and you should see a brighter red circle that stays there even when you move your mouse away:

Breakpoint added

We have now inserted a breakpoint at line 3 of the code!

Starting the Debugger

The easiest way to start the debugger is via the Run menu at the top. (The menu order is likely File, Edit, Selection, View, Go, Run, Terminal, Help.) Click to open the Run menu and choose Start Debugging. This will start the debugger.

If you just hit the main run / play button instead, it’ll run the program as usual and ignore any breakpoints. This corresponds to the Run menu option Run Without Debugging.

Stepping Through Code With the Debugger

You should now see this:

Execution stopped in the debugger

In the upper-left, under Variables, you can see a listing of the values of the variables we’re working with. You can also click any > symbol to expand that part, though in this simple program there’s not much else to see.

There are many features of a debugger. Let’s examine a few. In the top middle, are these buttons:

Debugger buttons

You can hover your mouse pointer over each button to get a hint about what it is. The leftmost button, Continue, will just let the code run again. It will keep running until it hits a breakpoint again or the program terminates. In this case, the program will just terminate.

The Step Over button (right arrow) is basically just “go to the next line” and for the moment behaves similarly to the Step Into button (down arrow). More details later. For now, click the Step Into button and you’ll notice that the debugger indicates you’re on line 4 now. Also note in the upper-left area that the value of x is now shown to be 6.

The Restart button (circle arrow) starts the program over again. The Stop button (red square) stops the debugger.

Note: If you hit Stop but the debugger seems to still be going, just hit Stop again. Sometimes it’s possible to have multiple debugger instances running at once.

Stepping Through Code That Includes Our Own Functions

If you’re reading this at a time when we haven’t yet discussed writing functions, then you may not fully understand the code we’re about to look at. That’s fine! Try this out now and know that we’ll cover everything in class soon; then you can try this out again.

First, remove the breakpoint you’ve placed by clicking the red dot to the left of line 3. Then copy the following code into your file (overwriting what was there before):

def my_function():  
    z = 5  
    z = z + 1  
    print(z)

def main():  
    x = 5  
    y = x + 2  
    x = x + 1  
    my_function()  
    print(x)  
    print(y)

if __name__ == '__main__':  
    main()

Put a breakpoint at line 9 (x = x + 1) and run the debugger. When the debugger stops at that line, click Step Into and observe the result. Keep clicking Step Into, observing the result after each click. You’ll notice that when in my_function, the x and y variables are no longer shown under Locals in the Variables window, but after executing line 2 (z = 5), the variable z is shown. Keep clicking Step Into and observe that the program continues back in main once my_function completes.

Now do the same again except click Step Over repeatedly instead of Step Into. You’ll notice that the debugger seems to have skipped the execution of my_function. Actually, my_function did execute, it just did it behind the scenes and didn’t show you the details. Thus:

Use both as needed each time you’re debugging.

Similarly, if you’re stepping inside of a function (e.g., my_function) and decide you want to jump out of it and resume debugging in the calling function, you can hit the Step Out button (up arrow). Experiment with that by hitting Step Into until you’re inside of my_function, and then hit Step Out once.

Debugging Tips

So now you know how to use the main features of a debugger. Nice work! Now, effective debugging takes practice. You’ll get better and better at it. Here are some tips:

Use your computer, not just the autograder

Error messages are usually helpful

Error messages are not always helpful

Handling wrong output

Explain what’s happening

Stare at it for a minute or two; no longer

Don’t guess and check

Only make principled changes

Don’t do something weird to counteract something weird

Use the debugger and print and if statements

Here are some examples of helpful print statement for debugging. You don’t have to always do all of this, but sometimes this level of detail can be very helpful.

if (some_variable == wrong_value):
    print("Debug: some_variable is the wrong value:", some_variable)
print("=========================")  # To clearly show the start of a new section of the code
print("Debug: About to process the list")
for item in some_list:
    print("----------")  # To clearly show where each iteration begins
    print("Debug: about to process item:", item)
    # process the item here
print("Debug: Done processing the list")
print("=========================")
def some_function(arg1, arg2):
    print("----------")
    print("Debug: some_function called with arg1 =", arg1, "and arg2 =", arg2)
    # rest of function here
    print("Debug: Done with some_function")
    print("----------")
def compute_something(x):
    if x == special_value:
        print("Debug: compute_something called with special_value:", x)
    # rest of function here

Fully understand the expected behavior of code before you try to write it

Connect programming tasks to in-class work

Removing Special Characters


© 2026 Steven Bogaerts.

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Creative Commons License

All materials provided for this course, including but not limited to labs, projects, notes, and starter code, are the copyrighted intellectual property of the author(s) listed in the copyright notice above. While these materials are licensed for public non-commercial use, this license does not grant you permission to post or republish your solutions to these assignments.

It is strictly prohibited to post, share, or otherwise distribute solution code (in part or in full) in any manner or on any platform, public or private, where it may be accessed by anyone other than the course staff. This includes, but is not limited to:

To do so is a violation of the university’s academic integrity policy and will be treated as such.

Asking questions by posting small code snippets to our private course discussion forum is not a violation of this policy.