By now if you have been messing around with the programs you have probably found that sometimes the program does something you didn't want it to do. This is fairly common. Debugging is the process of figuring out what the computer is doing and then getting it to do what you want it to do. This can be tricky. I once spent nearly a week tracking down and fixing a bug that was caused by someone putting anx
where ay
should have been.
This chapter will be more abstract than previous chapters.
The first thing to do (this sounds obvious) is to figure out what theprogram should be doing if it is running correctly. Come up with sometest cases and see what happens. For example, let's say I have aprogram to compute the perimeter of a rectangle (the sum of the lengthof all the edges). I have the following test cases:
height | width | perimeter |
---|---|---|
3 | 4 | 14 |
2 | 3 | 10 |
4 | 4 | 16 |
2 | 2 | 8 |
5 | 1 | 12 |
I now run my program on all of the test cases and see if the program does what I expect it to do. If it doesn't then I need to find out what the computer isdoing.
More commonly some of the test cases will work and some will not. If that is the case you should try and figure out what the working ones have in common. For example here is the output for a perimeter program (you get to see the code in a minute):
Height:3Width:4perimeter = 15
Height:2Width:3perimeter = 11
Height:4Width:4perimeter = 16
Height:2Width:2perimeter = 8
Height:5Width:1perimeter = 8
Notice that it didn't work for the first two inputs, it worked for the nexttwo and it didn't work on the last one. Try and figure out what is in common with the working ones. Once you have some idea what the problem is finding thecause is easier. With your own programs you should try more test cases if you need them.
The next thing to do is to look at the source code. One of the most important things to do while programming is reading source code. The primary way to do this is code walkthroughs.
A code walkthrough starts at the first line, and works its way down until the program is done.while
loops andif
statements mean that some lines may never be run and some lines are run many times. At each line you figure out what Python has done.
Lets start with the simple perimeter program. Don't type it in, you are going to read it, not run it. The source code is:
height=int(input("Height: "))width=int(input("Width: "))print("perimeter =",width+height+width+width)
height = int(input("Height: "))
Height:
, waits for the user to type a string in, and then converts the string to an integer variable height.width = int(input("Width: "))
Width:
, waits for the user to type a number in, and puts what the user types in the variable width.print("perimeter = ", width + height + width + width)
(It may also run a function in the current line, but that's a future chapter.)perimeter =
, then it prints the sum of the values contained within the variables,width
andheight
, fromwidth + height + width + width
.width + height + width + width
calculate the perimeter properly?The next program we will do a code walkthrough for is a program that is supposed to print out 5 dots on the screen. However, this is what the program is outputting:
. . . .
And here is the program:
number=5whilenumber>1:print(".",end=" ")number=number-1print()
This program will be more complex to walkthrough since it now has indented portions (or control structures). Let us begin.
number = 5
while number > 1:
while
statements in general look at their expression, and if it is true they do the next indented block of code, otherwise they skip the next indented block of code.number > 1
is true then the next two lines will be run.number > 1
?number
was5
and5 > 1
so yes. while
was true the next line is:print(".",end=" ")
end=" "
exists the next printed text will not be on a different screen line.number = number - 1
since that is following line and there are no indent changes.number - 1
, which is the current value ofnumber
(or 5) subtracts 1 from it, and makes that the new value of number. So basically it changes number
's value from 5 to 4.while
loop, so we have to go back to thewhile
clause which iswhile number > 1:
4 > 1
the while loop continues.print(".",end=" ")
number = number - 1
while number > 1:
3 > 1
so the while loop continues.print(".",end=" ")
number = number - 1
while number > 1:
2 > 1
the while loop continues.print(".",end=" ")
number = number - 1
while number > 1:
1 > 1
is false (one is not greater than one), the while loop exits.print()
while number > 0:
Another way would be to change the conditional to:number >= 1
There are a couple others.You need to figure out what the program is doing. You need to figure out what the program should do. Figure out what the difference between the two is. Debugging is a skill that has to be practiced to be learned. If you can't figure it out after an hour, take a break, talk to someone about the problem or contemplate the lint in your navel. Come back in a while and you will probably have new ideas about the problem. Good luck.
Non-Programmer's Tutorial for Python 3 | ||
← Decisions | Debugging | Defining Functions → |