Step 2: Set a Breakpoint and Inspect
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
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.
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
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: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.
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.