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!")
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)
Sharing the pizzas...Traceback (most recent call last):
File "main.py", line 4, in <module>
share = pizzas / people
~~~~~~~^~~~~~~~
ZeroDivisionError: division by zeroNameError, 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.")
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.")
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.