Debugging Python with Ease: A Primer for the Modern Coder

Unravel Python Code Mysteries with Print Statements and VS Code Debugger

PythonistaSage
ViciPyWeb3
Published in
4 min readMay 22, 2023

--

The “bug” was caught using a spyglass illustration. Made by Pythonista using Bing.

Today, we are going to traverse the intricate labyrinth of Python debugging together, transforming the seemingly complex into the beautifully simple. Drawing from the renowned CS50’s Introduction to Programming with Python, we’ll journey through the art and science of debugging, unearthing its mysteries, and mastering its techniques.

Alright, folks, let’s buckle up and get ready to dive into the world of Python debugging. We’ll be putting on our detective hats, unraveling the mysteries of Python code, using print statements and a debugger tool within our trusty sidekick, the Integrated Development Environment (IDE) — think of it as your coding command center, something like Visual Studio Code (VS Code).

We’ll be exploring the intriguing process of diagnosing issues in our code, and then, with the precision of a surgeon, we’ll employ our debugger to correct them. By the end of this tutorial, you’ll be armed with the tools and skills to dissect, diagnose, and debug your Python code like a seasoned pro.

And here’s the fun part — once you’ve mastered these skills, you might not need to rely on ChatGPT or any other chatbot for debugging. You’ll be commanding the ship, steering your code toward the shores of success. So, let’s hit the road and start this adventure!

So, without further ado, let’s go.

Step 1: Understanding the Problem

Consider this scenario. You have a Python program that prints a pyramid of hashes (‘#’) based on user input. Here’s the code:

def main():
height = int(input("Enter the height of the pyramid: "))
pyramid(height)

def pyramid(n):
for i in range(n):
print('#' * i)


if __name__ == "__main__":
main()

Imagine you’re running this program and enter 3 for the height, only to find that it prints a pyramid of height 2, not 3. Now, why does this happen?

Try to solve this by yourself…

… and the answer is…

… because the range function starts from 0, not 1. So, the pyramid function prints 0 hashes in the first line, 1 hash in the second line, and 2 hashes in the third line.

Step 2: Debugging with Print Statements

One of the simplest yet powerful tools at your disposal is the print statement. This can help you see the values of variables at different points in your program. Let’s modify the pyramid function to include some print statements:

def pyramid(n):
for i in range(n):
print("i is", i)
print('#' * i)

Running the program now will reveal the value of `i` before each line of the pyramid. This is your first step towards understanding why the pyramid is one line shorter than expected.

Step 3: Debugging with a Debugger

For more nuanced insights, we turn to a debugger. This tool lets you pause your code at specific points, step through it line by line, and see the values of variables at each step.

Take Visual Studio Code, for example. First, you set a breakpoint by clicking to the left of a line number. Then a red dot appears. Check the following screenshot.

Then, running your code with the debugger (by clicking the “Run and Debug” button) will cause it to pause at each breakpoint. From there, you can step through your code using the buttons at the top of the screen:

- The “Step Over” button (an arrow going over a dot) executes the current line and then pauses at the following line.
- The “Step Into” button (an arrow pointing down at a dot) steps into the function called on the current line, if there is one.

In our example, you could set a breakpoint at the start of the `for` loop in the pyramid function. I put after the definition of the function for example. As the debugger pauses at this line, you can observe the value of `n` in the “Variables” panel. Then, stepping through the loop will allow you to see how `i` and the pyramid change with each iteration. I

“f you need a simpler example that elucidates the use of the “Step Over” and “Step Into” functions in the Visual Studio Code (VS Code) debugger, you can check out this link (Debugging in Action: A Detailed Walkthrough)

Step 4: Fixing the Problem

With a clear understanding of the problem, we can proceed to fix it. In our case, we need to print one more hash on each line. This can be achieved by adjusting the line in the pyramid function to `print(‘#’ * (i + 1))`.

And voila! Now, when you run the program and enter 3 for the height, it prints a pyramid of height 3, just as expected.

Final thoughts…

You’ve done it! You’ve sharpened your debugging prowess, turning a coding challenge into a valuable learning experience. Always remember, debugging is not just a technical skill, it’s an art. It’s a problem-solving exercise that shapes you into a better coder with every hurdle you cross.

From my own journey, I can tell you that print statements have been my loyal companions. They are incredibly useful. But beware, forgetting to remove them can lead to unexpected results in your code. It can turn into quite a puzzle, figuring out which print statements were meant for testing and which ones were intended to stay.

Debugging, like any form of art, requires practice to perfect. So, the next time you stumble upon a bug, greet it with curiosity and confidence. Remember, every bug is just a mystery waiting to be unraveled.

Here’s to many more debugging adventures. Enjoy the journey!

Happy debugging! ;)

--

--

PythonistaSage
ViciPyWeb3

Skilled Python developer, educator. Passion for empowering women. Shares her expertise to make Python accessible to all, building a inclusive tech community.