Avoiding Conditional
Bugs in Python
Learn to write bulletproof conditionals by mastering condition order, boundary checks, and the subtle traps that cause silent failures in your code.
What Is a Conditional Bug?
A conditional bug is a logic error in an if/elif/else chain that causes your program to take the wrong branch. The code runs without crashing, but it produces incorrect results for certain inputs.
The Two Root Causes
Why Does Condition Order Matter?
Python evaluates if/elif chains top to bottom and executes only the first branch whose condition is True. It then skips all remaining branches, even if later conditions are also true.
The Golden Rule of Condition Order
Boundary Checks: \(<\) vs \(<=<\/span>
A boundary check bug happens when you use \(<\) instead of \(<=<\/b> (or \(>\) instead of \(>=<\/b>). The exact boundary value falls through to the wrong branch.
Overlap vs Gap in Conditions
| Overlap | Gap | |
|---|---|---|
| Cause | Conditions share a subset | No condition covers boundary |
| Symptom | Wrong branch executes | Unexpected else / no action |
| Fix | Narrow conditions or reorder | Add missing \(<=<\/b> or extra elif |
A Step-by-Step Debugging Checklist
Example 1: Buggy Grade Assignment
if score >= 60: # Too broad: catches 60-100
return "D"
elif score >= 70: # Never reached for 70-100!
return "C"
elif score >= 80:
return "B"
elif score >= 90:
return "A"
else:
return "F"
Example 1: Fixed Grade Assignment
if score >= 90: # Most specific first
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60: # Most general last
return "D"
else:
return "F"
Example 2: Off-by-One Boundary Bug
if age < 12: # Child: 0-11
return 5
elif age < 65: # Adult: 12-64
return 12
elif age > 65: # Senior: 66+
return 8
if age < 12:
return 5
elif age < 65:
return 12
else: # 65+: catches boundary!
return 8
Example 3: isinstance Ordering Trap
In Python, bool is a subclass of int. This means isinstance(True, int) returns True. If you check for int before bool, booleans are misclassified.
if isinstance(val, int): # Catches bool too!
return "integer"
elif isinstance(val, bool): # Never reached
return "boolean"
if isinstance(val, bool): # Specific subclass first
return "boolean"
elif isinstance(val, int):
return "integer"
Example 4: Compound Condition Pitfall
When combining and / or, operator precedence and short-circuit evaluation can hide bugs.
if age >= 13 and age <= 17 or has_consent:
print("Access granted")
print("Access granted")
Common Mistakes to Watch For
Bug Pattern Summary Table
| Pattern | Bug | Fix |
|---|---|---|
| Order of conditions | Broad condition first | Most specific first |
| Boundary value | \(<\) instead of \(<=<\/td> | Decide which branch owns the boundary |
| Gap in ranges | Value matches no branch | Add else or adjust operators |
| Subclass check | Parent class before child | Check child class first |
| Compound logic | Missing parentheses | Use explicit parentheses |
Question 1 of 5
if x > 0: print("positive") elif x > 100: print("large") else: print("non-positive")
What does this code print when \(x = 150\)?
Question 2 of 5
def fee(age): if age < 18: return 0 elif age > 18: return 10
What does fee(18) return?
Question 3 of 5
In Python, isinstance(True, int) returns True. To correctly distinguish booleans from integers, which should you check first?
Question 4 of 5
Which fix best prevents a gap bug in a series of numeric conditions?
Question 5 of 5
if temp >= 90: label = "hot" elif temp >= 70: label = "warm" elif temp >= 50: label = "cool" else: label = "cold"
Is this code correct, or does it contain a conditional bug?