All Python errors
SyntaxError in Python: invalid code structure
SyntaxError means the code breaks the grammar of Python, so nothing runs at all. To fix it, look at the line named in the message (and the one above): usually a colon, a bracket or a quote is missing.
Code with the error
temperature = 25
if temperature > 20
print("Warm")
Error message (Python 3.12, last line)
Corrected code
temperature = 25
if temperature > 20:
print("Warm")
Output
Warm
Common causes
- No colon after if, else, for, while or def.
- A bracket or a quote is opened and never closed.
- One = where a comparison needs ==.
- A reserved word such as class or for used as a variable name.
Practise in the course
Lesson: ConditionsFrequently asked questions
Why does the message point at the wrong line?
Python notices the problem only when it cannot continue, so the real mistake may sit one line higher.
Why is nothing printed before the error?
Python reads the whole program before running it. With a syntax error no line is executed.