Lesson 21 of 21 · 2 min
Common Errors & Fixes
| Error Message | Why it happens | How to fix |
|---|---|---|
SyntaxError: expected ':' | Forgot the colon at the end of an if, elif, else, for, or while line | Add : at the very end of the control-flow line |
IndentationError: expected an indented block after 'if' statement | The body of an if or loop has no indentation — Python cannot tell what belongs inside | Indent every line inside the block with 4 spaces |
SyntaxError: invalid syntax (using = in a condition) | Used a single = (assignment) inside a condition instead of == (comparison) | Replace = with == inside the if condition |
| Program freezes / prints forever | The while loop condition never becomes False because the variable is never updated inside the loop | Add a line inside the loop that changes the variable, e.g. count += 1 |
TypeError: 'str' object cannot be interpreted as an integer | Passed a string (e.g. from input()) directly to range() instead of an integer | Convert first: range(int(user_input)) |
| Off-by-one: loop runs one time too few | range(5) generates 0, 1, 2, 3, 4 — it stops before 5, not at 5 | Use range(1, 6) to get 1 through 5 inclusive |
