← Back to course
Code Crew G6+ / Week 01 / Friday
5/6
Week 01 Β· Hello, Mission Control

Friday

Reading an error calmly
// Your first real Python programs
⏱ about 30 min

Friday: Reading an error calmly

Rocket presses F5 on his new fuel report and the shell fills with red text. He pushes back his chair. "It is broken," he says. "Everything is broken."

Raven rolls her chair closer and reads the red lines from the bottom up. "What do you notice?" she asks. "It is not everything. The very last line names one problem, and another line gives a line number."

Nova hovers beside the screen and highlights one word. "Let's debug together," she says. "Python is not angry. It is telling you exactly where it got stuck."

Rocket takes a deep breath, pulls his chair back in, and reads the message slowly from the bottom.

Two kinds of errors

A syntax error means Python cannot even read the program, like a sentence with a missing quote mark. IDLE shows a box called SyntaxError and colors the spot in red. Nothing runs at all.

A runtime error happens while the program runs. The lines before the problem run fine. Then Python stops and prints a traceback in the shell.

print("Fuel check)

This one line has a syntax error. The string starts with a quote mark but never ends with one. Python says: unterminated string literal. Unterminated means it never ends.

Reading a traceback

print("Fuel report")
print("Tank A:", 40)
prnt("Tank B:", 35)
print("Done")

When you run it, the shell shows this. Your copy will show the full path to your file, and older versions of Python leave out the Did you mean part.

Fuel report
Tank A: 40
Traceback (most recent call last):
  File "fuel_report.py", line 3, in <module>
    prnt("Tank B:", 35)
    ^^^^
NameError: name 'prnt' is not defined. Did you mean: 'print'?
  1. Read the last line first. It names the kind of error and the problem.
  2. Find the line number in the line that starts with File.
  3. Look at that line in your program. The ^ marks point at the trouble.
  4. Fix one thing, save, and run again.
What kind of error is it? (one word)
Which line number has the problem?
WHAT HAPPENED WHEN IT RAN?
  • Read the question.
  • Tap your answer.
Which lines of output appeared before the error?
Did Done get printed?
Line 3 says prnt. What word should it say instead?
WHY THIS EXERCISEPython only knows the name print. A typo in a name gives a NameError.
At your computer
1. Make a new file and type fuel_report.py exactly as shown, typo included.
2. Save it in your mission folder and press F5.
3. Read the last line of the red text first. Then find the line number.
4. Fix prnt to print, save, and run again. All four lines should appear.
5. Now delete the closing quote mark after Done. Press F5 and read the SyntaxError box.
6. Put the quote mark back and run once more.

Practice: two more errors

Python names are case sensitive. That means print and Print are different names to Python, so a capital letter can cause a NameError.

A missing round bracket is a syntax error. Python reports that a bracket was never closed, and nothing in the file runs.

print("Status board")
Print("All clear")
print("End of report")
DETECTIVE WORK
  • Read the question.
  • Tap your answer.
Which line number has the problem?
What is wrong with line 2?
You type print("Ready" and run it. What kind of error appears?
How many lines of output appear before the error?
Does End of report get printed? (yes or no)
StatementTrue or false?
print and Print mean the same thing to Python.?
With a syntax error, no lines of the file run.?
With a runtime error, lines above the problem still run.?
The first line of a traceback names the kind of error.?
WHY THIS EXERCISENames are case sensitive, syntax errors stop everything, and the error name is on the last line.

Every programmer sees error messages every day, and reading them calmly is a real skill. You finished your first week as Mission Commander. Tomorrow is a Crew Quest.

← Thursday