Brainlag
  1. Library
  2. Debugging
  3. Python

SyntaxErrorWhat is a SyntaxError?

A SyntaxError means Python could not read your code at all, so nothing ran. A missing colon, bracket or quote is the usual reason.

Python5 min

What SyntaxError means

Output
SyntaxError: expected ':'

Before running anything, Python reads the whole file and checks that it is written in valid Python. A SyntaxError means that check failed, so not a single line ran, not even the ones above the mistake.

The usual causes:

  • A missing colon at the end of if, for, while, def or else.
  • A bracket or quote that was never closed. print("hi" or "hello.
  • = where == was meant, as in if score = 10:.

How to find it

Python shows the line with a ^ under the spot where it got confused. The real mistake is often there, or at the end of the line just above it, because an unclosed bracket only becomes a problem when the next line starts. Check that line for a missing :, ) or quote.

Run it: a missing colon

Press Run and read the error from the bottom up.

main.py to run
score = 12
if score > 10
    print("High score!")

The scoreboard that never starts

This should print the best score and the number of games. Nothing runs at all. Fix it.

exercise_1.py to run
scores = [12, 30, 7]
print("Best:", max(scores)
print("Games:", len(scores))

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.