Making Friends With Errors

Why red text is good news, not a disaster

7 min read · runnable Python

Here's a secret every programmer knows: errors are normal. You will see them constantly, forever, no matter how experienced you get. They are not a sign you're bad at this. They're the computer trying to help.

An error message means Python got confused and stopped to tell you where. The trick is to read it instead of panicking.

What an error looks like

If you run something Python can't handle, you get a message ending in a line like:

NameError: name 'mesage' is not defined

Read it backwards: it found mesage (a typo for message) that was never defined. The last line of an error is usually the most useful — it names the kind of problem.

Three errors you'll meet early

  • SyntaxError — a typing mistake, like a missing bracket or quote. Python couldn't even read your code.
  • NameError — you used a name Python doesn't recognise, often a typo or a variable you forgot to create.
  • TypeError — you tried to mix types in a way that doesn't work, like "5" + 5 (text plus number).

How to fix one

  1. Read the last line — what kind of error is it?
  2. Look at the line number it mentions.
  3. Check that line for typos, missing quotes, or missing brackets.
  4. Change one thing, run again, repeat.

Tip: Don't try to fix five things at once. Change one thing, run, see what happens. Small steps make problems obvious.

Your turn

The code on the right has a deliberate typo that causes a NameError. Run it, read the message, then fix the typo and run again. Breaking things on purpose is one of the best ways to learn.

Try it yourself
Output
Press “Run code” to see the result.

Quick check

Q1 You see NameError: name 'totl' is not defined . What's the most likely cause?

Q2 Which part of an error message is usually the most helpful to read first?

Want to save your progress? It's free.

Create a free account