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:

Syntax errors:

Look back at previous programs if you need an example of correct syntax for a particular function or procedure.

Logic errors:

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?

Danger

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.

You have attempted of activities on this page