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.

Step 2: Set a Breakpoint and Inspect

00:00 In the previous lesson, you took an educated guess in which section of the code the bug may be. In this lesson, you’ll set a breakpoint and inspect the code by stepping through it with your debugger. Previously, you concluded that the error is probably somewhere inside of your for loop.

00:19 So you will go ahead and set a breakpoint at line 3, and you do that by right-clicking or Control-clicking and selecting Set Breakpoint.

00:28 You can see that IDLE highlights the code for you, and now you can run your program and start debugging it. I’ll go and press F5. You can see that the interactive window tells me that it restarted with the debug still being on, and the Debug Control window now gives me information about my program. It stopped at line 1,

00:55 so just before executing any code in the script. Now, because you set a breakpoint, you can press the Go button to jump through the function definition and the main part of your code, where it calls the function, and stop just before executing line 3.

01:13 You can see that, opposite to what you’ve seen when you stepped through the non-program before, now you actually have the Locals panel filled in with the local scope variables inside of your add_underscores() function.

01:27 And you can see that in here you’ve defined a variable called new_word that points to a string that is just an underscore and also that the phrase "hello" was defined outside of the scope.

01:40 You can see that in the Globals panel. And then it was passed to the function as

01:45 the variable word, and it’s available in the local scope of the function

01:50 under the variable word, and it points to the string "hello". So these two variables are defined inside of your function. And at this point, the code is paused just before entering the for loop in the underscores function. You can now click Step to go to the next line,

02:08 and in this case, you’ve executed line 3 and the code stopped before executing line 4. You can see that the first character has been identified and it’s an 'h'.

02:21 You can also switch on the Source checkbox to see where the code is currently. So you can see that now it’s waiting before executing line 4. You continue stepping, and what happened now is quite interesting.

02:36 You can see that the variable new_word changed from just an underscore to 'h_'. However, shouldn’t it be '_h'?. So it seems like something went wrong here.

02:50 Instead of adding to the existing string, it looks like you overwrote the string with the character plus an underscore. Let’s confirm this by continuing to step through the code.

03:04 If you press Step two more times, you’ll see that when the character is 'e', it makes the new_world variable point to the string 'e_'. Now, this is definitely not what you want.

03:16 If you keep stepping, you’ll see that it just keeps replacing new_word instead of adding to it.

03:23 So what you did in this step is you set a breakpoint on line 3, you started the debugger, and then you stepped through the relevant part of your code, where you guessed that the error might be, and you looked at the variables inside of the Locals and Globals panel and watched how they changed while you execute your program line by line. And by doing this, you identified that something’s going wrong, and you also identified where it’s probably happening. So in the next step, you have a chance to identify the error and then try to fix it.

Become a Member to join the conversation.