Lesson 11 of 35Python
Python · Lesson 11 · 30 minutes
Bug detective: finding bugs
What you'll learn
- Read error messages from the bottom up
- Find bugs that don't produce an error message
- Look inside a program with
print()and “Steps”
Bugs are part of the game
A mistake in a program is called a bug. The name supposedly goes back to 1947, when a real moth got stuck inside a computer. Since then, finding mistakes has been called debugging.
Important: everyone makes mistakes, professionals too, every day. Good programmers aren’t the ones who never make mistakes – they’re the ones who find them quickly.
Clue 1: the error message
When Python stops, the message is your most important clue. Read it from the bottom up:
prices = [3, 5, 2]
total = sum(prices)
print("Total: " + total)- The last line says what happened:
TypeError– text and a number don’t fit together. - Above it you can see which line it happened in.
- Codelix marks the line in red and explains the error. Try to fix it:
str(total)or a comma instead of+.
Codelix even spots typos: try typing pirnt("Hello") in an editor.
Clue 2: bugs without an error message
Nastier are bugs where the program runs through but produces the wrong thing:
# Should count how many numbers are bigger than 10
numbers = [5, 12, 30, 8, 11]
count = 0
for number in numbers:
if number > 10:
count = 1
print(count)The only thing that helps here: think beforehand about what should come out (here: 3), then compare. Can you spot the bug?
Clue 3: look inside the program
If you don’t know what’s going on, let the program show you:
- print() at suspicious places:
print("number:", number, "count:", count)inside the loop shows every step. - The ⏯ Steps button in the editor plays the program line by line and shows all variables.
total = 0
for i in range(1, 5):
total = total + i
print("i =", i, "total =", total)Detective rule: change only one thing at a time, then run it again. If you change five things at once, you won’t know what helped.
Quick check
What does this program print? And trickier: for which list would it give a wrong result?
values = [4, 7, 1]
biggest = 0
for v in values:
if v > biggest:
biggest = v
print(biggest)Your task
Your turn
This program has three bugs. average(marks) should work out the average of a list of marks, and best_mark(marks) should find the best mark – where (as with German school marks) the smallest number is the best. Run the program, read the error message and fix one bug after another.
def average(marks):
total = 0
for mark in marks:
total = mark
return total / len(mark)
def best_mark(marks):
best = marks[0]
for mark in marks:
if mark > best:
best = mark
return best
marks = [2, 1, 3]
print("Average:", average(marks))
print("Best mark:", best_mark(marks))- Something to think about
Read the last line of the error message.
len()needs the whole list – is it really called mark? - Which command helps?
The total should grow in the loop. total
=mark overwrites it every time – write total=total+mark.Here the smaller number is the better mark. Which comparison sign do you need then?
- Gap code – fill in the ___
def average(marks): total = 0 for mark in marks: total = ___ return ___ def best_mark(marks): best = marks[0] for mark in marks: if ___: best = mark return best marks = [2, 1, 3] print("Average:", average(marks)) print("Best mark:", best_mark(marks))
Solve it without hints to earn a ⭐ on your progress page.
Show solution🔒 Unlocks after all hints or 2 checks
Try it yourself first – mistakes teach you the most. Here is one possible solution:
def average(marks):
total = 0
for mark in marks:
total = total + mark
return total / len(marks)
def best_mark(marks):
best = marks[0]
for mark in marks:
if mark < best:
best = mark
return best
marks = [2, 1, 3]
print("Average:", average(marks))
print("Best mark:", best_mark(marks))