Lesson 19 of 21 · 3 min
Common Mistakes and Error Messages
IndentationError — Wrong or Missing Indentation
# MISTAKE: Not indenting the body of an if statement
score = 85
if score >= 60:
print("Pass") # ERROR: this should be indented
Error: IndentationError: expected an indented block after 'if' statement
# FIX: Indent with 4 spaces (or 1 Tab)
if score >= 60:
print("Pass") # correctly indented with 4 spaces
# MISTAKE: Mixing tabs and spaces for indentation
if True:
print("A") # indented with spaces
print("B") # indented with a Tab — Python sees this as DIFFERENT!
Error: TabError: inconsistent use of tabs and spaces in indentation
# FIX: Use ONLY spaces (4 spaces) or ONLY tabs — never mix them
if True:
print("A") # 4 spaces
print("B") # 4 spaces — consistent
SyntaxError — Missing Colon After if/elif/else/for/while
# MISTAKE: Forgetting the colon
if score >= 60 # MISSING colon at the end!
print("Pass")
Error: SyntaxError: expected ':'
# FIX: Always end if, elif, else, for, while with a colon
if score >= 60: # colon is required
print("Pass")
Infinite Loop — Condition Never Becomes False
# MISTAKE: Variable never changes, so loop runs forever
count = 1
while count > 0: # count is always 1, which is always > 0
print(count) # keeps printing 1 forever
# FORGOT: count -= 1 or count = count - 1
"Fix": Press Ctrl+C to stop the program, then add the update:
while count > 0:
print(count)
count -= 1 # decrease count each iteration so it eventually reaches 0Using = Instead of == in Conditions
# MISTAKE: Single = assigns, double == compares
x = 5
if x = 5: # this is an assignment, not a comparison — illegal here
print("five")
Error: SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
# FIX: Always use == for comparison inside if statements
if x == 5: # == checks if x equals 5
print("five")
off-by-one Error with range()
# MISTAKE: Expecting range(5) to include 5
for i in range(5): # range(5) gives 0, 1, 2, 3, 4 — NOT 5!
print(i) # prints 0, 1, 2, 3, 4 (not 5)
Output: 0 1 2 3 4
# FIX: Use range(1, 6) to get 1, 2, 3, 4, 5
for i in range(1, 6): # range(1, 6) gives 1, 2, 3, 4, 5
print(i)
