
PYTHON — Step Two Python Breakpoint
Technology offers us a unique opportunity, though rarely welcome, to practice patience. — Allan Lokos
Insights in this article were refined using prompt engineering methods.

PYTHON — More JavaScript Syntax Python
In the previous lesson, you made an educated guess about where the bug might be in your code. Now, you will set a breakpoint and inspect the code by stepping through it with your debugger. You previously concluded that the error is probably somewhere inside your for loop. Let's see how you can use the Python debugger to identify and fix the issue.
First, you will go ahead and set a breakpoint at line 3. You can do this by right-clicking or using the shortcut Control-click and selecting Set Breakpoint. Now, let’s run the program and start debugging it by pressing F5.
You can see that the interactive window tells you that it restarted with the debug still being on, and the Debug Control window now gives you information about your program. It stopped at line 1, 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.
At this point, you will notice that the Locals panel is filled in with the local scope variables inside of your add_underscores() function. You can see that you've defined a variable called new_word that points to a string that is just an underscore and that the phrase "hello" was defined outside of the scope.
You can also see in the Globals panel that it was passed to the function as the variable word, and it's available in the local scope of the function under the variable word, pointing to the string `"hello".
Now, the code is paused just before entering the for loop in the add_underscores function. You can click Step to go to the next line, and in this case, you've executed line 3, and the code stopped before executing line 4. You can switch on the Source checkbox to see where the code is currently and continue stepping.
As you step through the code, you will notice that the variable new_word changed from just an underscore to 'h_'. However, it should be '_h'. It seems like something went wrong here. Instead of adding to the existing string, it looks like the string was overwritten with the character plus an underscore.
By continuing to step through the code, you’ll see that when the character is 'e', it makes the new_world variable point to the string 'e_', which is definitely not what you want. It just keeps replacing new_word instead of adding to it.
In summary, by setting a breakpoint on line 3, starting the debugger, and then stepping through the relevant part of your code, you identified that something’s going wrong and also identified where it’s probably happening. In the next step, you will have a chance to identify the error and try to fix it.
By using breakpoints and stepping through your code, you can inspect the values of variables and the flow of your program, which can be incredibly useful for identifying and fixing bugs.
This method of debugging is essential for any Python developer, and it’s a valuable skill to have in your toolkit. As you continue to work with Python, you’ll find that using breakpoints and stepping through your code will become second nature, and you’ll be able to solve bugs more efficiently.
In the next lesson, you will have the chance to identify the error and then try to fix it. This process of setting breakpoints, inspecting variables, and stepping through code is an essential part of the debugging process, and it will help you become a more proficient Python programmer.
As you become more comfortable with using the debugger, you’ll find that you can locate and fix bugs in your code more quickly and with greater confidence. Debugging is a skill that takes time and practice to master, but it’s an essential skill for any programmer.






