Error guide · SyntaxError
SyntaxError: expected ':'
A line that starts a block is missing the colon at the end.
Example
for i in range(3) print(i)
What Python says
SyntaxError: expected ':'
As printed by Python 3.12. Other versions may word it slightly differently.
Why it happens
Lines that begin with if, elif, else, for, while, def or try open a block, and each must end with a colon. The colon tells Python that the indented lines below belong to it. Python reports the error on the line that needs the colon.
How to fix it
for i in range(3): print(i)
Output: 0 ⏎ 1 ⏎ 2
Tips
- Check the end of every
if,for,whileanddefline. elseandtryneed a colon too, writtenelse:andtry:.- Do not add a colon to ordinary lines inside the block.