Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

A Buggy Program

In this lesson, you’ll get started debugging a faulty function. To follow along, go ahead and type the following code into your editor:

def add_underscores(word):
    new_word = "_"
    for char in word:
        new_word = char + "_"
    return new_word


phrase = "hello"
print(add_underscores(phrase))

With that, you’re ready to go through the four steps of debugging!

00:00 Now that you’ve gotten comfortable with using the Debug Control window in IDLE, let’s take a look at a buggy program. You’ll see a piece of code that defines a function called add_underscores(), and the expected behavior is that if you pass in a string, for example "hello", you would get back a string that starts with an underscore (_) and ends with an underscore, and each letter also is separated from the next letter by an underscore. This is what the function’s supposed to do, but once you’ll run it, you see, it’s not exactly what’s happening.

00:36 So on this slide, you can see the code that you will work with. You can pause the lesson for a second and type this code into your editor. I will head over to IDLE, where I already have this code copy-pasted.

00:52 Now we’ll start a new debug session. Remember you need to select the interactive window, then go to Debug/Debugger.

01:05 And I will rearrange the windows so I have the same layout as before, with the editor in the top-left, the interactive window in the bottom-left, and then the Debug Control window here on the right.

01:19 So I’ll need to save this code. You can see that it’s called buggy.py and double-check that the debugger is on by looking at the interactive window. The message is there, so you’re all good and ready to go.

01:33 So I will run this code by pressing F5.

01:37 You can see that the debugger stops right away, and now I will use the Go button to run the script to the end so that we can see what it currently does without yet stepping through it.

01:48 If you don’t set a breakpoint and you press the Go button, then your script is just going to run from, start to finish. And here we are. The script finished running, and the output is o_.

02:01 So instead of getting _h_e, et cetera, you get only the final letter and an underscore at the end.

02:12 Which means that the expected output was, as you can see here, _h_e_l_l_o_, with separated by underscores, and the actual output of the buggy program is just the o and an underscore. This is not what you want, so it’s time to debug the script and figure out what’s going wrong.

02:33 For debugging this program, and any program that you’re going to work with, you can follow four steps. First, you want to guess which section may contain the bug. You might not be accurate, but you will be able to have an educated guess of approximately where the error might sit in your program. Next, you will set a breakpoint at this location and then start inspecting the code by stepping through it with your debugger. While you do that, you will keep track of important variables and their values and see how they change while you step through the code. Next, you will identify a possible error and then make a change that hopefully solves the problem. And finally, you will repeat steps one to three as needed until the code works as you expected it to work.

03:21 And these are the debugging steps that you’re going to follow for the script in this course, but you can also use them for any debugging that you will do throughout your life. So let’s get started with making an educated guess which section of the buggy code may contain the bug.

Become a Member to join the conversation.