Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
4.3. Errors in Programs¶
Below are some programs that have errors you need to fix. Errors in programs fall into two major categories. Syntax errors are errors in code that violate the rules of the language. Syntax errors will either prevent the program from running or prevent a particular line from running correctly. Logic errors are pieces of code that follow the rules of the language correctly, but do not do what the programmer actually wants.
Here are some examples of the two types of errors as we might see them in turtle programs:
Case matters in Python so
screenis not the same as
open and close parentheses
()are required after every function and procedure call, even if it doesn’t take any input.
frowardis not the same as
Look back at previous programs if you need an example of correct syntax for a particular function or procedure.
If instructions are in the wrong order, or missing, the turtle will not draw what we intended.
If a procedure call like
left(90)leaves out the angle -
left- or uses the wrong angle, the turtle will not draw what we intend.
4.3.1. Debugging Programs¶
A major part of writing programs is debugging them - finding and fixing bugs, or errors, in the code. Debugging code is often time harder than writing it in the first place. It is easy for a human to read code and mentally fill in gaps - to make assumptions about how the code will work that are not actually true.
So how do you debug a program?
Read carefully. Slow down, read each line out loud.
Think about what each line is trying to do. Try to explain each line in English and think about what it is trying to accomplish.
Make sure each line of code is doing what you intended. To do this, you can:
Use a tool like the Codelens to run the program one line at a time. Examine all of the variables before and after each line runs. (Unfortunately, Codelens does not work for turtle programs.)
If you are working with strings or numbers, add
print()commands to print out the result of each line of code. Instead of doing a bunch of calculations and then printing out the final answer, print out the result of each step.
Related to the idea of verifying each line is the idea you should focus your attention on one small
part of the program at a time. Do your best to isolate one part of the code from everything else
so there is less to worry about. One way to do this is by commenting out code. Commenting out
code means putting a
# at the start of one or more lines of code to turn them into comments so
the computer ignores them.
In the program below, we have commented out lines 5-7. Try running the program - it won’t try
to do anything after the
alex.forward(150). Because it successfully creates the turtle and
does the move forward, we are pretty sure those lines are working correctly.
The full program is supposed to draw a square, but there are some bugs.
Try turning one line of code at a time back on by removing the
# at the start. Then run the
program. Does the line seem to do its job? If so, go to the next line. If not, stop and try to fix
that line before moving on.
The following example has 3 errors. Can you fix the errors so that the code runs correctly to print a capital L? You may want to start by commenting out all the lines and turn them on one by one like we did in the last exercise.
The following example has 4 errors. Can you fix the errors so that the code runs correctly to print a capital C?
One of the errors in this one is tricky. It won’t cause an immediate problem, but will cause future lines to misbehave. Errors like this are especially hard to debug. If a line of code is not doing its job, and you are 100% certain it is right, you may have to go re-examine earlier lines of code that you thought were correct.
Use the area below to try to draw a block letter or number.