Skip to content

Reading Output and Error Messages

By the end of this lesson you'll be able to:

  • Tell the difference between normal output and an error message just by its color
  • Read what an error message says to figure out where your program went wrong
  • Use print() checkpoints to narrow down exactly which line caused a crash

When your Python program runs, two kinds of text can appear in the output box. Normal output — anything printed with print() — appears in black. Error messages — produced when Python finds a problem — appear in red. The color makes it easy to see at a glance whether your program finished successfully or crashed.

Welcome to This Lesson!

Monty waving welcome Every programmer sees error messages — even professional ones! Learning to read them calmly is one of the most useful skills you'll build. Let's code it together!

Normal Output: Everything Goes Right

When a program runs without any problems, every print() call produces a line of black text in the output box. Run the program below and look at the output — all black, no red.


  

All five lines print in black — the program ran from top to bottom without any problems.

Error Output: Something Went Wrong

Now look at what happens when Python hits a problem partway through a program. The program below prints two lines successfully, then tries to use a variable called high_score that was never created. Python cannot continue, so it stops and reports the error.

What Will the Output Look Like?

Monty thinking Before you click Run, count the print() calls in the program below. How many lines of output do you think will appear — and what color will each one be? Make your prediction, then click Run!


  

Were you right? Two black lines appeared for the two successful print() calls. Then a red error message appeared — Python stopped when it hit the broken line. The last print() never ran at all because the program crashed before it got there.

How to Read a NameError

The red message says something like:

1
NameError: name 'high_score' is not defined on line 8

Here is what each part means:

Part Meaning
NameError The type of error — Python found a name it doesn't recognise
'high_score' The exact variable name Python couldn't find
on line 8 The line number where the crash happened

To fix this error you have two choices:

  1. Create the variable before you use it — add high_score = 100 somewhere above line 8.
  2. Remove or fix the broken line — if high_score shouldn't be there, delete it.

The Black Lines Are Your Clues

Monty with a tip When a program crashes, look at the last black line in the output — that tells you the most recent thing that worked. The error happened somewhere between that line and the next print() you expected. Use this as your starting point when hunting for the bug!

Using Print Checkpoints to Find Bugs

Professional programmers often add temporary print() calls to trace exactly where a program stops. Each one says "I made it this far." When you see the last black checkpoint and then a red error, you know the crash happened between those two points.

Here is the same broken program with checkpoints added. Notice how the checkpoints help you pinpoint the problem line more precisely:


  

All four checkpoints print in black — the program is healthy. Now remove the # from in front of print("High score:", high_score) and run it again. Checkpoint 3 will be the last black line, and the red error will appear right after it — telling you the crash happened between Checkpoint 3 and Checkpoint 4.

Red Doesn't Mean Failure — It Means Information

Monty warning A red error message is not a sign that you are bad at coding. It is Python telling you exactly what went wrong and where. Read it carefully — it almost always contains the line number and the name of whatever Python couldn't find. The message is your friend!

Experiments

Try these changes. For each one, predict the output color pattern before you run!

  1. In Lab 2, add high_score = 500 on a new line above print("High score:", high_score). You'll know it worked when all output is black and "High score: 500" appears.

  2. In Lab 2, change score = 42 to score = "forty-two" (a word instead of a number). Then change print("Score:", score) to print("Score + 1:", score + 1). You'll know it worked when you see a red TypeError — Python can't add a word and a number.

  3. In Lab 3, remove the # from the broken line and run it. Count the black checkpoint lines in the output. You'll know it worked when you can identify which checkpoint was last and explain why Checkpoint 4 never appeared.

  4. In Lab 1, rename total to final_total in the calculation but leave print("Total:", total) unchanged. You'll know it worked when a red NameError for total appears after the first three black lines.

You Can Read Error Messages!

Monty celebrating Black means success, red means "look here." You now know how to read both — and how to use print checkpoints to hunt down a bug. That's real debugging skill! Let's code it together!