Skip to content

Module 5 · Conditional Statements — topic 10 of 10

Common mistakes

A practical review of the conditional-statement bugs beginners hit most: = versus ==, wrong range order, separate ifs instead of elif, string comparisons, floating-point equality and unreachable branches.

Beginner 14 min read·Lesson 69 of 306 in Python Programming

Learning objectives

  • Recognise the most common conditional bugs
  • Diagnose a faulty condition by testing boundary values
  • Rewrite buggy conditions correctly

Definition

A practical review of the conditional-statement bugs beginners hit most: = versus ==, wrong range order, separate ifs instead of elif, string comparisons, floating-point equality and unreachable branches.

Introduction

Conditions are small but they hide many bugs, often ones that only appear for particular inputs. This lesson collects the mistakes seen most often in beginners’ code, explains why each happens, and shows how to catch it with a quick test.

Why it matters

A condition that is wrong for one boundary value can give a student the wrong grade or charge a customer the wrong price. Recognising these patterns lets you spot them while writing code and when reviewing someone else’s.

Detailed explanation

Assignment instead of comparison: if x = 5 is a SyntaxError in Python, which at least makes it easy to find. Use ==.

Overlapping ranges in the wrong order: in an if-elif chain the first true branch wins, so broad conditions must come after narrow ones.

Separate ifs where elif was meant: several branches run and later ones overwrite earlier results.

Incomplete or-comparisons: x == "a" or "b" is always true. Write x in ("a", "b").

Case and whitespace in text: "Yes" == "yes" is False. Normalise with .strip().lower() first.

Comparing numbers stored as text: "10" < "9" is True. Convert before comparing.

Exact float equality: 0.1 + 0.2 == 0.3 is False. Use math.isclose().

Unreachable or redundant branches: after if x > 10, an elif x > 20 can never run.

The cure for most of these is the same: test boundary values and unusual inputs, not just one typical case.

Syntax

Text
if value == target:
if choice in ("a", "b"):
if answer.strip().lower() == "yes":
if math.isclose(total, expected):
  • == compares; = assigns.
  • in replaces chains of or.
  • Normalise text before comparing.
  • Compare floats with a tolerance.

Code examples

The always-true or

Python
colour = "green"
print(bool(colour == "red" or "blue"))
print(colour in ("red", "blue"))
Output
True
False

Text versus numbers

Python
print("10" < "9")
print(int("10") < int("9"))
Output
True
False

More examples

Finding a bug with boundary tests

Python
def band_buggy(marks):
band = "fail"
if marks >= 40:
band = "pass"
if marks > 75:
band = "distinction"
return band
def band_fixed(marks):
if marks >= 75:
return "distinction"
elif marks >= 40:
return "pass"
return "fail"
for m in [39, 40, 74, 75, 76]:
b, f = band_buggy(m), band_fixed(m)
flag = "" if b == f else " <-- differs"
print(m, b, f, flag)
Output
39 fail fail 
40 pass pass 
74 pass pass 
75 pass distinction   <-- differs
76 distinction distinction 

Step-by-step explanation

  • band_buggy uses separate ifs and > 75, while the rule says 75 and above is a distinction.
  • band_fixed uses an elif chain with >= 75 checked first.
  • The loop tests values on each side of both boundaries.
  • Only the exact boundary 75 reveals the difference, which is why boundary tests matter.

Expected output

Running “Finding a bug with boundary tests” prints exactly:

TextOutput
39 fail fail
40 pass pass
74 pass pass
75 pass distinction <-- differs
76 distinction distinction

Practical applications

  • QA teams write test cases for exact boundary values such as minimum age or maximum quantity.
  • Code reviewers watch for or-chains and float equality in conditions.
  • Payment systems normalise currency codes and text before comparing them.

Best practices

  • Test both sides of every boundary.
  • Normalise and convert input before comparing.
  • Prefer if-elif chains for mutually exclusive outcomes.

Common mistakes

Testing only typical values

Avoid

Checking the grade function with 50 and 90 only.

Prefer

Also test 39, 40, 74, 75 and 76.

Most condition bugs live exactly at the boundaries.

Comparing floats exactly

Avoid

if price * qty == 59.97:

Prefer

if math.isclose(price * qty, 59.97):

Binary floats can differ in the last digit.

Key takeaways

  • Use == for comparison and in for alternatives.
  • Order ranges from narrow to broad in elif chains.
  • Normalise text, convert numbers, and compare floats with a tolerance.

Try it yourself

  1. 1
    Find and fix the bug: if day == "Saturday" or "Sunday": print("weekend").
  2. 2
    Write boundary tests for a rule “free entry for ages under 5 or 65 and over”.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

2 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is wrong with `if x = 5: print(x)`?
  • BeginnerWhat is printed?
Practise this topic