Section 16.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 wants.
Here are some examples of the two types of errors as we might see them in turtle programs:
Syntax errors:
- Case matters in Python so
turtle
is not the same asTurtle
- open and close parentheses
()
are required after every function and procedure call, even if it doesn’t take any input. - Spelling matters.
froward
is not the same asforward
.
Look back at previous programs if you need an example of correct syntax for a particular function or procedure.
Logic errors:
- 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.
Subsection 16.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 - and to make incorrect assumptions about how the code will work!
So how do you debug a program?
- Read carefully. Slow down and 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 2 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 then turn them back on one by one like we did in the last exercise.
The following example has multiple errors. Can you fix the errors so that the code runs correctly to print a capital C?
Use the area below to try to draw a block letter or number.
You have attempted of activities on this page.