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.
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
- == compares; = assigns.
- in replaces chains of or.
- Normalise text before comparing.
- Compare floats with a tolerance.
Code examples
The always-true or
True False
Text versus numbers
True False
More examples
Finding a bug with boundary tests
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:
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
Prefer
Most condition bugs live exactly at the boundaries.
Comparing floats exactly
Avoid
Prefer
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
- 1Find and fix the bug: if day == "Saturday" or "Sunday": print("weekend").
- 2Write 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?