1E: Errors

As you do more and more programming, you will naturally encounter a lot of errors (or bugs). Causing, understanding, and fixing errors is an important part of programming. Python will do its best to run anything that you tell it to run, but if it can't understand what you're asking, then it won't run the program. All the same, Python will try to tell you a little bit of information about what went wrong, in order to help you try to fix it.

Here are two Python errors.

Example: A Syntax Error

In this first example, we forget to use the parenthesis that are required by print(...). Python does not understand what you are trying to do.

Here is a second example of a bug in Python.

Example: A Run-Time Error

In the second example, we forget to define the greeting variable. Python knows what you want it to do, but since no greeting has been defined, an error occurs.

A syntax error happens when Python can't understand what you are saying. A run-time error happens when Python understands what you are saying, but runs into trouble when following your instructions.

In English, a syntax error would be like the sentence

Please cat dog monkey.

The grammar of this sentence does not make sense. From the perspective of English grammar, it is missing a verb (action). You cannot understand what you are being asked to do. Syntax means the same thing as grammar.

In English, a run-time error would be like the sentence

Please eat the piano.

The sentence makes sense from a grammatical perspective — there is a verb and noun in the right place — so you know what you are being asked to do. But, you will encounter problems once you start trying to eat the piano (except maybe if you are a termite). This is called a run-time error because it occurs after the program starts running.

We also will talk about logic errors at the end, which means that your program runs without crashing, but still produces the wrong result. An example would be

Please close the back door so that the bugs don't come in.

This would be a logic error if the front door is also open: even though the command makes sense and you can close the back door, this doesn't actually accomplish the goal of keeping the bugs out since they'll still come in the front door.

Common Syntax Errors in Python

Here are a few additional examples of syntax errors that can occur in Python. One very general thing that can occur is that Python will encounter a special symbol in a place that it doesn't expect.

Example: Syntax Error

Python says SyntaxError: invalid syntax and points with ^ to the exclamation point. The problem is that ! has no meaning in Python. The syntax error would go away if we had put print("Hello, World!") instead, because then Python would understand that the ! is part of the text with Hello, World.

Here is another syntax error that is more subtle.

Example: Syntax Error

The problem is that class is a special word in Python. if you had written course instead of class it would have been fine. Click here to see the list of all special "keywords" in Python.

If you are using quotes around text and you forget the second one, or you are using parentheses and forget the second one, you will get syntax errors:

Example: Syntax Error
Forgetting the second quote

In this error, EOL is short for End Of Line: Python expected another " but the line ended before it was found.

Example: Syntax Error
Forgetting the second parenthesis

Similarly, EOF is short for End OFile: Python kept looking for a ) but the program file ended before it was found.

Sometimes two very similar syntax errors can give two very different error messages (example). But, every error message is indeed trying to tell you something helpful.

Run-Time Errors

Here are a few common run-time errors. Python is able to understand what the program says, but runs into problems when actually performing the instructions.

  • using an undefined variable or function. This can also occur if you use capital letters inconsistently in a variable name: 
    Example
    An undefined variable
  • dividing by zero, which makes no sense in mathematics. (Why? Since 0 times any number is 0, there is no solution to 1 = X * 0, so 1/0 is undefined.)
    Example
    Dividing by zero
  • using operators on the wrong type of data
    Example
    Adding text and a number

You will find more ways of producing errors as you learn more about Python.

What is the technical difference between syntax and run-time errors? Here is an example comparing a run-time error to a syntax error. Look at the output of each program.
Example: Run-Time Error
Example: Syntax Error
The program with the run-time error created some output, but the one with the syntax error did not. This is because Python runs in two steps:
  1. Python checks if your program has correct syntax, in order to determine its structure and parts.
  2. If no syntax errors were encountered in step 1, then the program is executed.

So, a program with a syntax error will execute no steps at all, but a program with a run-time error will execute the steps that happened before the error occured.

Logic Errors

Your program might run without crashing (no syntax or run-time errors), but still do the wrong thing. For example, perhaps you want a program to calculate the average of two numbers: the average of x and y is defined as

\displaystyle{\frac{x+y}{2}}

Why doesn't this program work?

Example
This does not calculate the average correctly.

The average should be

\displaystyle{\frac{x+y}{2}=\frac{3+4}{2}=\frac{7}{2}=3.5}

but the program prints 5.0 instead! The error this time has to do with the "order of operations" in arithmetic. When you write x + y / 2, this has the same mathematical meaning as x + (y / 2) = 3 + (4 / 2) = 3 + 2 = 5. To fix the problem, the third line of our program should be written as average = (x + y) / 2, which makes clear to Python that we really want the value \frac{x+y}{2}, where we add first and divide afterwards.

You can have logic errors because you designed a program incorrectly, or because you didn't write code that follows the design correctly (like the average example). Logic errors can be difficult to spot, especially in a longer program, but as you get better at writing code you will also get better at avoiding logic errors. Lesson 6D will give some tips on avoiding logic errors.

Exercises

Now that the lesson is complete, we have three exercises on debugging (fixing errors in programs). You can try to spot the errors before running the programs, or you can run them first and use Python's response to determine what needs fixing.

Each program comes with some code already set up for you. You only need to change a few characters (letters/symbols/numbers) to fix each program. The grader will reject any solution that changes too many characters. Select Reset code to default if you want to bring back the original version.

Coding Exercise: Summer
Fix the syntax error in the following program, so that it prints out the sum of all the numbers from 1 to 10. You can change at most one character.

Coding Exercise: Hello Joe
Fix the run-time error in the following program, so that it prints out Hello on the first line and Joe on the second line. You can change at most two characters.

Coding Exercise: Shopping
You are going shopping for meat and milk, but there is tax. You buy $2.00 of milk and $4.00 of meat, and the tax rate is 3%. Print out the total cost of your groceries (you don't need to print the dollar sign). Hint

Once you have squashed all of the bugs, move on to the next lesson!