0%

Welcome to Python Land · practice

Errors Are Part of Programming

Errors appear early in programming, so it is worth learning what they really mean.

An error does not mean you are bad at programming. It means Python reached something it could not understand or carry out. The error message is Python’s report of what happened.

Experienced programmers see errors every day. The useful skill is not avoiding every error. It is knowing what to do when one appears.

Create an error on purpose

Run this example once while it is still correct:

Try it

Now remove only the closing quotation mark after the period and press Run again.

Python cannot tell where the text ends, so it will show an error. You may see wording similar to:

SyntaxError: unterminated string literal

Read the message, then press Reset code to restore the working example.

You just created an error, inspected it, and recovered from it. Nothing was damaged.

Start at the bottom

An error report can contain several lines. You do not need to understand all of them yet. The final line is usually the best place to begin.

Look for:

  • The error name, such as or , on the final line

  • A short message on that line describing what Python could not do

  • A line number elsewhere in the report showing where Python noticed the problem

A SyntaxError means Python could not understand the structure of the code. A missing quotation mark or parenthesis can cause one.

A NameError means Python encountered a name it does not know. Python names are case-sensitive, so Print and print are different names.

The next example contains a line beginning with #. That is a comment: a note for a person reading the code, which Python does not carry out. Here it tells the playground which intentional error to expect.

Try it

Python knows the lowercase print, but it does not know a function named Print. The error may also suggest print as a possible correction. Even without a suggestion, the unknown name tells you where to start comparing your code with a working example.

A debugging routine you can reuse

When a program produces an error, try this sequence:

  1. Stop and read the final line of the error.

  2. Find the line Python mentions.

  3. Compare that line with an example you know works.

  4. Change one likely problem.

  5. Run the program again.

Changing one thing at a time matters. If you change five things at once, you will not know which change solved the problem.

Your program shows an error. What is the best first response?

Repair the program

The playground for this lesson contains three print instructions. One of them uses the wrong capitalization.

Press Run before changing anything. Python will complete the first line, stop at the second line, and show a NameError. The third line will not run because Python stopped at the error.

Find the unknown name and correct it. The finished program must print:

Errors are useful.
I can read them.
I can fix them.

Do not remove the troublesome instruction. Repair it, run the program again, and then press Submit.

Let Monty translate the message

While the error is visible, you can press Ask Monty to explain my error below the output. If you have already repaired the program, you can try this with another error later.

Monty can translate the message, connect it to your code, and give you a smaller next step. You still make the repair, which is where the learning happens.

Errors are not detours from programming. Finding and fixing them is programming.

Task

Run the starter program and read the error. Then fix the capitalization mistake so the output is exactly:

Errors are useful.
I can read them.
I can fix them.

Run the repaired program, then press Submit.