Python Arena
Step 4 of 4

Errors and debugging

The syntax of a programming language is its set of rules for how code is written: an if line ends with a colon, a block is indented, a string has quotes at both ends. When code breaks a rule, Python can't read the program, so it runs none of it and reports a syntax error:

speed = 50
if speed > 40
    print("Slow down!")
Python stops with
  File "main.py", line 2
    if speed > 40
                 ^
SyntaxError: expected ':'

A program with correct syntax can still go wrong. An error that happens while the program runs is a runtime error. Python stops at the line it can't carry out, and the lines before it have already run:

pizzas = 3
people = 0
print("Sharing the pizzas...")
share = pizzas / people
print(share)
Output
Sharing the pizzas...
Python stops with
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    share = pizzas / people
            ~~~~~~~^~~~~~~~
ZeroDivisionError: division by zero

NameError, TypeError and ValueError are runtime errors too. The traceback names the line that was running, and its last line says what went wrong. The real cause may be somewhere else, though: here the mistake is that people is 0.

Bugs and debugging

The trickiest mistakes cause no error at all: the program runs, but its result is wrong. A mistake like that is a bug, and finding and fixing bugs is called debugging. In this game, every full 1000 points earn a bonus life, and 3 bonus lives or more earn a medal. A player with 3000 points should get the medal, but doesn't:

points = 3000
lives = points % 1000
if lives > 3:
    print("Medal earned!")
print("Game over.")
Output
Game over.

A simple and very effective way to debug is to add debugging prints: extra print lines that show the values of variables and conditions while the program runs. Here the program never shows how many lives it worked out, or what it decided about the medal, so print both, each with a label:

points = 3000
lives = points % 1000
print("lives:", lives)
print("medal condition:", lives > 3)
if lives > 3:
    print("Medal earned!")
print("Game over.")
Output
lives: 0
medal condition: False
Game over.

Now you can check each value against what you expected. 3000 points are three full thousands, but lives is 0, so the line that calculates it is wrong: % gives the remainder, what's left over after the full thousands. The number of full thousands is points // 1000. With that fixed, the prints say lives: 3 but still medal condition: False. The value is right now, so the condition must be wrong: "3 or more" is lives >= 3, and lives > 3 leaves out exactly 3.

Notice that 3000 was a good number to try. Bugs like > instead of >= only show up right at a limit, so test with the limits themselves as well as with ordinary values. And when the bug is fixed, remove the debugging prints, or the checks on this site will see them in the output.

Your task

Challenge 1 of 3

This program has four syntax errors. Fix them one at a time (Python reports one error at a time), so the program works like this:

How many tickets? 6
Group discount!
Total: 50 euros

Challenge 2

This quiz never says Correct!, not even when the answer is right. Add debugging prints to find out why: print the value of the condition, and the type of answer. Then fix the bug and remove the debugging prints. With the right answer it prints:

What is 6 * 7? 42
Correct!
Thanks for playing.

Challenge 3

This program should print the average of two exam scores, but it stops with an error. Fix it. Once it runs, check its result too: the average of 80 and 90 is 85, and there's a second bug.

First score: 80
Second score: 90
Average: 85.0