← All Python errors

Python error explained

SyntaxError: what it means and how to fix it

Python could not read your code at all, so nothing ran.

The message

SyntaxError: expected ':'

What it means

Python reads the whole file before running any of it. A SyntaxError means it hit something that is not valid Python, so it stopped before the first line ran. The arrow (^) in the message points at where Python got confused, which is often just after the real mistake.

Common causes

  • A missing colon at the end of if, elif, else, for, while, def or class lines
  • A missing closing bracket or quote, which makes the error appear on the next line
  • Using = (store) where you meant == (compare) inside an if
  • A stray character, like a smart quote pasted from a document

Example that raises it

score = 72
if score >= 50
    print("Pass")
else:
    print("Try again")

The fix

score = 72
if score >= 50:
    print("Pass")
else:
    print("Try again")

Tip: If the line Python points at looks fine, look at the line above it. An unclosed bracket or quote there is the usual culprit.

Fix the broken version yourself in the editor on this page. It runs real Python in your browser.

Start the free lessons More Python errors