← Back to course
Python 11-12 / Week 01 / Thursday
4/6
Week 01 · Back at the Terminal

Thursday

Three kinds of error
// Review fast, trace before you run, and name the three kinds of error
⏱ about 30 min

Thursday: Three Kinds of Error

Comet pins three printouts to the board. Each one is a bug from the crew's first week.

"One would not start at all," she says. "One stopped in the middle. And one ran fine but gave the wrong total. That last one is the sneakiest!"

Wren studies the three pages. "What does the evidence say?" he asks. "They are different kinds of mistake, so they need different ways to find them."

Nova projects three labeled bins on the wall. "How can I help?" she asks. "Read each program, then sort it into a bin."

Comet hands you the first page. "Lead programmer, which bin?"

Three kinds of error

  • A syntax error is a mistake where the rules of the language are not followed.
  • A run-time error happens while the program runs. An exception, like KeyError, is one kind of run-time error.
  • A logic error is a mistake that makes the program behave wrongly. Testing finds it: you compare what it does with what it should do.

A syntax error

# syntax_demo.py
print("Start")
grams = [42, 18, 65, 7]
for g in grams
    print(g)
  File "syntax_demo.py", line 4
    for g in grams
                  ^
SyntaxError: expected ':'

Look closely at the run. Start never printed, even though print("Start") is on line 2.

The mistake was found before the first line ran. A compiler checks code for some errors in the same way, before the program runs.

READ THE SYNTAX ERROR
  • Read the question.
  • Tap your answer.
What is missing from line 4?
Why did Start not print?

A run-time error

# index_demo.py
grams = [42, 18, 65, 7]
print("Last:", grams[3])
print("Next:", grams[4])
Last: 7
Traceback (most recent call last):
  File "index_demo.py", line 4, in <module>
    print("Next:", grams[4])
                   ~~~~~^^^
IndexError: list index out of range

IndexError means a list index is out of range. The list has four items, and the run shows grams[3] worked while grams[4] stopped the program.

READ THE RUN-TIME ERROR
  • Read the question.
  • Tap your answer.
Which line printed before the error?
Which line number does the message point to?

A logic error

This program is meant to total all four samples, 132 g. It runs with no message at all. Compare its output with what it should show.

# logic_demo.py
grams = [42, 18, 65, 7]
total = 0
for i in range(len(grams) - 1):
    total += grams[i]
print(f"Total: {total} g")
Total: 125 g
FIND THE LOGIC ERROR
  • Read the question.
  • Tap your answer.
Which number does the loop leave out?

The fix is to loop over range(len(grams)). The run of the fixed version matches the expected total.

# logic_demo.py, fixed
grams = [42, 18, 65, 7]
total = 0
for i in range(len(grams)):
    total += grams[i]
print(f"Total: {total} g")
SORT EACH BUG INTO A BIN
  • Read the question.
  • Tap your answer.
for g in grams with no colon at the end.
grams[4] on a list of four items.
A total that leaves out the last sample.
Comet's KeyError from Wednesday.
HUNT DOWN A LOGIC ERROR
  • ?Fix the line and run it again
  • ?Run the program
  • ?Work out what the output should be
  • ?Trace the code to find where they split
  • ?Compare the real output with the expected output
WHY THIS EXERCISETesting finds a logic error by comparing what the program does with what it should do; tracing then shows where.
At your computer
1. Type syntax_demo.py, save it in your fieldsim folder and run it. Check that Start never prints.
2. Add the colon and run it again. Predict the output first.
3. Type index_demo.py and run it. Change grams[4] to grams[-1]. Predict, then run it.
4. Type logic_demo.py and run it. Fix the range and check for 132 g.

Every error kind has its own clue: a message before the start, a message partway, or a wrong answer. Tomorrow is Trace Friday.

← Wednesday