From the course: Programming Foundations: Beyond the Fundamentals

Introduction to debugging - Python Tutorial

From the course: Programming Foundations: Beyond the Fundamentals

Introduction to debugging

- I'm trying to use my drill, but it doesn't work. To figure out what's wrong, and if it's a problem I can fix I might need to go through some testing. Is the battery all the way in? Yep. Is the battery charged up? Well, can swap out, grab a battery that I know just came off the charger, still no love. Oh wait, the switch doesn't move when I push it. I engaged the safety lock. If I switch that off, the drill works. After you write a program, it has to be executed in order for you to see the results. But even the best developer writes code from time to time that includes errors or bugs. Just like a broken drill, a computer program with a bug doesn't run as expected or may not run at all, but not all bugs are the same. The process of identifying and fixing bugs is known as debugging and it's an important skill for every programmer. There are three main types of bugs in computer programs. A program has a syntax error when it includes code that doesn't match the rules of the language. For instance, if I wrote a conditional in Python without including after the if statement, my code wouldn't execute because the Python interpreter wouldn't be able to identify the end of that particular statement. It's the same thing if I misspell a word like if I say P-I-R-N-T instead of print. Sometimes your code can comply with all the rules of the language, but still not execute successfully. For instance, suppose you include a call to a function that isn't ever defined. A function call is a valid statement, but when the interpreter tries to run your code it won't be able to find and execute the function specified in your function call. This type of bug is known as a runtime error because it often isn't obvious until the code is actually run. A third type of error happens when you write code that runs, but doesn't produce the results you expect. For instance, a loop that counts in the wrong direction continues infinitely, and as a result, the program never completes execution. This is known as a logic error because while the statements themselves constitute valid code, the result of the statements isn't what you were trying to achieve when you wrote the code. Debugging is just a part of programming. Every programming language has tools available that help programmers identify bugs, and in some cases suggest how to fix them. But using those tools successfully requires that you take a methodical approach to identifying and then fixing errors. When your code doesn't run as you expect, remember that it may contain more than one bug, however execution will generally fail at just one point. You can work with the feedback provided by your tools to fix the indicated bug, but afterwards, it's important to retest and follow up on any other error messages or other issues that crop up after that first bug is fixed. You'll need to repeat this cycle until your program finally executes successfully and works as you intend. Rather than seeing debugging as a chore, I try to look at it as an interesting puzzle to solve. I encourage you to approach debugging as an opportunity to learn more about programming in general and about the specific language you are working in.

Contents