← Back to Blog

Common IF Mistakes

The problem...

Your code runs. No errors. But the output is wrong.

Or it crashes on something that looked perfectly fine.

These are the mistakes almost every beginner makes with if. At least once.

Mistake 1 — Using = instead of ==

# Wrong
if score = 90:
    print("Excellent.")
# Right
if score == 90:
    print("Excellent.")

= assigns a value. == compares. Python throws a SyntaxError immediately — this one is easy to spot once you know it.

Mistake 2 — Missing the colon

# Wrong
if score >= 60
    print("Passed.")
# Right
if score >= 60:
    print("Passed.")

The colon is not optional. Every if, elif, and else line ends with :. No colon — SyntaxError.

Mistake 3 — Wrong indentation

# Wrong
if score >= 60:
print("Passed.")
# Right
if score >= 60:
    print("Passed.")

Python uses indentation to define blocks. No indent — IndentationError. Always use 4 spaces.

Mistake 4 — Wrong order in elif

# Wrong — broad condition first
if score >= 60:
    print("Passed.")
elif score >= 90:
    print("Excellent.")    # never reached
# Right — most specific first
if score >= 90:
    print("Excellent.")
elif score >= 60:
    print("Passed.")

Python stops at the first match. Put the most specific condition first — always.

Mistake 5 — Comparing strings with the wrong case

window = input("Window seat? (y/n): ")
# Wrong — user types "Y", condition fails
if window == "y":
    price += 3
# Right
if window.lower() == "y":
    price += 3

Python is case-sensitive. "y" and "Y" are not the same. .lower() normalizes the input before comparing.

Mistake 6 — else catches more than you think

score = 110

if score >= 90:
    print("Excellent.")
elif score >= 60:
    print("Passed.")
else:
    print("Failed.")    # runs for 110 — is that correct?

else catches everything that didn't match above — including values you didn't expect. Always think about what your fallback actually covers.

What's really happening

Most of these mistakes are not about not understanding if.

They're about assumptions. You assume = works for comparison. You assume Python doesn't care about case. You assume else only catches the obvious cases.

It doesn't. Be explicit. Always.

The mindset shift

Stop thinking: "This should work."

Start thinking: "Let me check exactly what Python expects."

What you should understand now

  • = assigns, == compares — never mix them in a condition
  • Every if, elif, else line ends with :
  • Indentation is not style — it's syntax
  • Most specific conditions always go first
  • String comparisons are case-sensitive — .lower() is your friend
  • else catches everything — make sure you know what that includes
[ login to bookmark ] // copied! 34 views · 2 min
// resources
Other if_mistakes.py
← prev IF Mini Project — Your ticket, your price next → if / elif / else — The Full Picture
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.