Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Python Basics: Finding and Fixing Code Bugs (Summary)

In this chapter, you learned about IDLE’s Debug window. You saw how to inspect the values of variables, insert breakpoints, and use the Step, Go, Over, and Out buttons.

You also got some practice debugging a faulty function using a four-step process for identifying and removing bugs:

  1. Guess where the bug is located.
  2. Set a breakpoint and inspect the code.
  3. Identify the error and attempt to fix it.
  4. Repeat steps one through three until the error is fixed.

Debugging is as much an art as it is a science. The only way to master debugging is to get a lot of practice with it!

If you’d like to learn more about the concepts covered in this course, then check out:

Download

Sample Code (.zip)

561 bytes
Download

Course Slides (.pdf)

2.5 MB

00:00 Congratulations. You’ve made it to the end of this Python Basics course about finding and fixing code bugs. Did you see how you just fixed that code bug? Nice work.

00:12 While going through the lessons of this course, you first learned about how to use IDLE’s Debug Control window, and then you practiced debugging on a buggy function.

00:23 To get started with IDLE’s Debug Control window, you first need to open it, and you can do that from the interactive window by going to the menu option Debug and then click Debugger. That opens up the Debug Control window.

00:36 And you will also see that there’s the string [DEBUG ON] displayed in the prompt of your interactive window. Once you see that, you know you’re good and you can get started debugging.

00:49 You then also learned about the user interface elements in IDLE’s, Debug Control window. You learned about the different buttons—Step, Out, Over, Go, and Quit—of which Step is the one that goes line by line, Out takes you out of a function, Over steps over a certain function, Go runs until the next breakpoint, and Quit stops the debugger without exiting the Debug Control window.

01:16 You learned about the four checkboxes on the top-right of the Debug Control window. Those are Stack, Locals, Globals, and Source, of which three of those are directly related to a panel in the Debug Control window. Those are Stack, Locals, and Globals.

01:33 And you also learned about what you can see inside of these panels while you debug your program.

01:41 Then you learned about the four debugging steps. You start by guessing where the bug is located. Then you set a breakpoint and inspect the code. You identify the error and attempt to fix it.

01:52 And then you repeat steps one to three until the error is actually fixed. After going over these four steps in theory, you also practiced them on a buggy program. And the buggy program you worked with had a function called add_underscores() that takes a string and should return a string with each character separated with underscores.

02:14 Instead, it only returned the final letter and an underscore. So you went into this buggy program and worked on fixing it, following the four steps, and you managed to debug it, which means that the actual output was the same as the expected output, and you have a non-buggy program at the end. Yay! Congratulations.

02:38 Finally also learned about print debugging, which means adding print() calls to inspect variables at different states of your program. And you learned that this can be useful in systems with limited resources, for example IoT devices, if they don’t have a debugger that’s available to you. However, it means there’s more code to write for you.

02:59 It means that you always need to run the whole program to see the output. And you also need to remember to remove the print() calls afterwards.

03:06 So if you can, use a debugger. While using IDLE’s debugger, you might have noticed that you weren’t able to interact with the variables inside of your program. You were just able to view them.

03:18 Now there’s also something called interactive debuggers, and Python’s standard library comes with one of them and that one is called Pdb. Real Python has also resources on working with Pdb, So if you’re interested in diving deeper into debugging, check out this course on Python Debugging With Pdb

03:38 and the associated tutorial that is also about debugging in Python using Pdb. And one final thing I want say: even though you can be harsh to bugs inside of your code, and some people like to talk about squashing them, just keep in mind that real-life bugs are actually really cute and you don’t want to hurt those. All right, thanks for joining me in this course on finding and fixing code bugs.

04:03 I hope you learned something, and see you the next time.

Avatar image for Ed Schneider

Ed Schneider on Aug. 5, 2022

Excellent tutorial with one important flaw. It never explains what “IDLE” is. Even after going through the tutorial the user has no idea where to find it. Does is come with Python?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Aug. 5, 2022

@Ed Thanks for the comment. This is a part of a video course series based on the Python Basics book, which uses IDLE from start to finish. You can check out one of the earlier video courses, which introduces you to IDLE. And yes, it comes with Python by default.

Avatar image for Ed Schneider

Ed Schneider on Aug. 6, 2022

@Bartosz Thanks for clarifying. It might be helpful to point users to those other tutorials. Even better would be to revise this tutorial to help the user find/get to IDLE even when they don’t have the menu bar (or interactive window) the tutorial refers to.

Avatar image for akazimierz

akazimierz on Aug. 11, 2022

Thanks for a tutorial providing me with a firm grasp of debugging with IDLE – seems as if I know almost everything about it (definitely much more than before (Because I’m ashamed, I won’t admit that I’ve never used IDLE debugging capability and only printing messages…)).

BTW – I was thinking about a function allowing for debugging with an ‘off switch’, for the production mode. I came with something like:

def printd(message, DBG=True):
    print("DBG: " + str(message))  # the simplest form

Along with functools.partial it grants the debugging and allows for flexibility.

Regards,

Khaz.

Become a Member to join the conversation.