Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Debugging

Unit - I
What Is Debugging?
Debugging is trying to find why your code doesn’t
behave the way you want it to.
A bug is a mistake or problem that is causing the code to
fail at its goal.
Debugging is the process of finding and removing those
bugs.
Debugging is one of the most fundamental parts of
programming.
It’s the flow you go through when you’ve written some
code to check if it behaves as desired—or fix it if it
doesn’t.
What Is Debugging?
Debugging in the Programming Flow
Types of Errors
Compilation Errors
Errors can differ significantly from language to language, but
there are some common ones. For compilation errors, some
things you might often encounter are:
Syntax errors. You’ve written code that just doesn’t make
sense in the current programming language. This is akin to a
grammatical error in an essay.
Name errors. You’ve written code that tries to use something
that doesn’t exist. Imagine, for example, I asked you, “Give her
the book,” but never specified who “her” is. The command
doesn’t make sense because I’m including a person that doesn’t
exist.
Type errors. You’ve written code that tries to do something
that doesn’t make sense, like requesting the smell of True or the
color of the number 5.
Types of Errors
Runtime Errors
Runtime errors most often occur because of something
specific to the results that code generates when it runs.
Some of the common runtime errors you will encounter
are:
Divide by zero errors. Your code contains a number being
divided by another, but when those numbers actually have
values, it turns out you’re trying to divide by zero!
Null errors. Null errors are like name errors: you’re
referring to something that doesn’t exist.
Memory errors. Your computer can only remember a
certain amount of stuff at a time. If you try to require it to
remember more than that, you’ll hit a memory error.
Types of Errors in Python
NameError
A NameError occurs when you use a variable name
that doesn’t exist. Don’t worry too much about what
variables are right now.
Types of Errors in Python
TypeError
A TypeError is one type of our error where we’re
trying to do something that doesn’t make sense.
Types of Errors in Python
AttributeError
An AttributeError is the other result when we try to
do something in our code that doesn’t make sense.
Types of Errors in Python
SyntaxError
The last common type of error we’ll cover for now is
the SyntaxError.
A SyntaxError is kind of a catch-all error: it refers to
lots of different things that can be done wrong, all
based on violating Python’s internal grammar.
Basic Debugging
Print Debugging
The simplest and most common type of debugging you’ll
use early on is called print debugging.
A form of debugging where print statements are added
throughout the code to check how the program is flowing.
Scope Debugging
A form of debugging where print statements are added to
check the status of the variables in the program at different
stages to see how they are changing.
Rubber Duck Debugging
This is where rubber duck debugging comes in. Rubber duck
debugging was introduced by the 1999 book The Pragmatic
Programmer, and it refers to a programmer who carried
around a rubber duck to which to explain problems.
Basic Debugging
Print Debugging in Python
Let’s start with a short code segment. The goal of the code
segment in Figure 1.3.6 is to count from 1 to some number
(in this case, 10), and then back from that number to 0. At
the end, the code should print out the final number.
Basic Debugging
Print Debugging in Python
In Figure 1.3.7, we’ve put print statements throughout the
code. That way, we can look at the output and see where the
code is getting stuck. We see that it completes the first loop,
starts the second loop, but never finishes the second loop.
Now we know that the problem is on line 8 or 9. That’s far
easier to solve than not knowing at all where the problem is.
Basic Debugging
Print Debugging in Python
In Figure 1.3.8, we revised line 9 to subtract instead of add,
ran the code again, and received the output we expected. At
this point, we might go back and remove our print
debugging statements; they were only there to help us
resolve the problem, and now it’s resolved!
Basic Debugging
Scope Debugging in Python
let’s imagine a little program that calculates the average
from a number of grades. The grades themselves are shown
in the first line of Figure 1.3.9: 100, 95, 93, and so on.
In this case, we can manually calculate the average of those
grades to know what output we expect: we add those
numbers together on a calculator, divide by 12, and we find
the average should be 89. However, right now the code in
Figure 1.3.9 is outputting 6.833… so something isn’t
working. How do we find out what’s wrong?
Basic Debugging
Scope Debugging in Python
If we’re using print debugging, we might add print
statements to make sure that the program is running through
everything as expected, as seen in Figure 1.3.10.
Basic Debugging
Scope Debugging in Python
In Figure 1.3.11, we’re printing the sum each time we add a
new grade to it. So, we can calculate what sum should be at
any time: 100 after one run, 195 after two, 288 after three,
and so on. Our first line appears correct, sum is 100 after
one run. After two runs, though, it’s 95 instead of 195.
After three, it’s 93 instead of 288. So, we’ve found the
problem: sum isn’t being computed properly. The only place
sum is being modified is line 7, so we know the problem is
on line 7. Now we can look and see the issue (though we
might not understand it until Unit 2): sum isn’t adding the
next grade, it’s just being replaced by it!
Basic Debugging
Scope Debugging in Python
Basic Debugging
Scope Debugging in Python
We fix that in Figure 1.3.12 by adding grade to the current
sum, then we run the code again, and voila. We receive the
correct result, 89.0. Now we could remove our debugging
statements, and we’d be left with code that accomplishes the
goal we set out to accomplish.
Advanced Debugging
Some of the advanced tools available for debugging
include:
Step-by-step execution. Some development environments
will allow you to run your code one line at a time.
Variable visualization. Some development environments
will show you a simple chart of all the data stored in
memory.
In-line debugging. Some development environments are
sophisticated enough to debug simple errors while you’re
actually writing the code.
Advanced Debugging

You might also like