Syntax Errors

A syntax error means Python could not parse the program.

Parsing happens before the program runs. If Python cannot understand the shape of the code, it cannot execute even the first line.

The condition is missing a colon. Python has not reached a bad value. It has not entered the if branch. It cannot read the code as Python.

Common syntax errors

Missing colon

Blocks after if, for, while, def, and similar statements need a colon:

Unclosed string or bracket

name = "Ada

Python keeps looking for the closing quote.

scores = [8, 6, 9

Python keeps looking for the closing bracket.

Indentation that changes meaning

Indentation is part of Python syntax. This is valid:

This is not:

The indented line is the body of the loop. Without it, the loop has no body.

Fix syntax first

When there is a syntax error, fix that before reasoning about program logic. Python may point slightly after the real mistake, especially with missing brackets or quotes. Look at the indicated line and the line just before it.

Exercise: Fix the missing syntax

Repair the program so it prints pass.

Fix the missing syntax

Runs locally with Python in your browser.

Ready to run.

HintRepair the block boundary

A block-opening if line ends with a colon, and its body is indented.

SolutionAdd the colon and indentation

Write if score >= 7: and indent print("pass") beneath it. Python can then parse the block, the condition is true, and the program prints pass.

Syntax versus logic

This code has correct syntax:

It may still be logically wrong. Syntax only means Python can read the code. It does not mean the code says what you meant.

That distinction matters. First make the program readable to Python. Then make the program correct.