← Back to course
Code Crew G6+ / Week 10 / Tuesday
2/6
Week 10 Β· Bugs and tests

Tuesday

Know your errors
// Tracebacks, try and except, and assert
⏱ about 30 min

Tuesday: Know your errors

Rocket pins a big sheet of paper to the wall and writes every error name he has seen so far. The list is longer than he expected.

"It is like a field guide," Raven says, delighted, and adds a column for what each one means. "What do you notice?" she asks. "Each name tells you the kind of problem before you even read the rest."

Nova adds one more row at the bottom, with no error name in it at all. "Interesting," she says. "The trickiest bugs give no error. The program runs, but the answer is wrong."

Rocket frowns at the empty row. "How do you find a bug that does not announce itself?" he asks.

Nova blinks. "You make the program show you what it is doing."

A field guide to errors

ErrorUsually meansExample
SyntaxErrorPython cannot read the lineprint("hi" (missing bracket)
IndentationErrorThe spaces at the start are wrongno indent after if x:
NameErrorA name is misspelled or not made yetprnt("hi")
TypeErrorTypes that do not mix"Fuel: " + 50
ValueErrorRight type, unusable valueint("ten")
IndexErrorNo item at that positionplanets[10]
KeyErrorNo such key in a dictionaryprobe["speed"]
ZeroDivisionErrorDividing by zero5 / 0
FileNotFoundErrorNo file with that name hereopen("misssing.txt")
NAME THAT ERROR
  • Read the question.
  • Tap your answer.
crew = ["Lee", "Ana"]. What does crew[2] cause?
What does int("3.5") cause?
You type Print("hi") with a capital P. What happens?
scores = {"Ana": 5}. What does scores["Lee"] cause?

Bugs with no error message

A logic bug runs without any error, but gives the wrong answer. Python cannot spot it, because the code is valid. You have to notice that the output is wrong.

The best tool is a test you can check by hand, plus a temporary print that shows the values inside the loop.

# count_full.py (has a logic bug)
tanks = [80, 50, 30, 95]
full = 0
for t in tanks:
    print("checking", t)   # temporary debug print
    if t > 50:
        full = full + 1
print("Tanks at 50 or more:", full)
checking 80
checking 50
checking 30
checking 95
Tanks at 50 or more: 2
By hand, how many tanks are at 50 or more?
Which comparison sign should replace > to fix it?
At your computer
1. Type count_full.py, save it and run it. Compare the answer with your count by hand.
2. Fix the comparison and run it again.
3. Delete the debug print once the program is right.
4. In the shell, cause three errors on purpose from the table. Read each last line.

Practice: an off by one bug

One of the most common logic bugs is being off by one. A range that stops one number too early gives an answer that looks almost right.

# sum_bug.py (should add 1 to 10, but has a bug)
total = 0
for n in range(1, 10):
    total = total + n
print("Total:", total)
HUNT THE HIDDEN BUG
  • Read the question.
  • Tap your answer.
The sum of 1 to 10 is 55. Why does the program show 45?
Which line fixes it?
Which debug print would show the problem fastest?
What kind of bug is this?
What does the fixed program show? Total: ____
What does int("seven") cause? (error name)
What does 10 / 0 cause? (error name)
StatementTrue or false?
A logic bug always shows a red traceback.?
Checking against an answer worked out by hand can reveal a logic bug.?
A temporary debug print should be deleted when the bug is fixed.?
"Fuel: " + 50 causes a TypeError.?
WHY THIS EXERCISELogic bugs give no error, so you compare with a hand answer and use debug prints to look inside.

You can now name most errors at a glance, and hunt bugs that hide. Tomorrow you will teach your programs to handle errors without stopping.

← Monday