Python Basics for Vibe Coders

Python Basics for Vibe Coders, Part 3: if, elif, else and the Order Bug

Python if/elif/else runs only the first true branch. Why condition order turns 95 into a B, = vs. ==, and testing AI conditionals with boundary values.

Python Basics for Vibe Coders, Part 3: if, elif, else and the Order Bug

Any code that behaves differently depending on the situation, like "90 or above is an A, 70 or above is a B," is built with conditionals. When an AI writes this kind of code, it can look fine and still give wrong results because of a single detail: the order of the conditions. This is Part 3 of the 10-part series "Python Basics for Vibe Coders." It covers comparison operators and if / elif / else, plus how to check conditionals in AI code. The examples are written for Python 3.15, and I ran them in Python 3.14.2 to check the results.

Illustration of a glowing path splitting three ways on a dark floor, with a blank signpost at the fork and only one of three blue, orange, and gray lights switched on

Key Takeaways

  • The indented code under a condition runs only when that condition is true.
  • if / elif / else checks from the top and runs only the first branch that's true, then stops.
  • That's why the stricter (narrower) condition has to come first. Get the order wrong and a 95 becomes a B.
  • == compares, = assigns. if x = 3: is a syntax error.

A conditional is a signpost at a fork in the road

Think of a traffic light. If it's red, you stop. If not, you check whether it's green, and if it is, you go. If it's neither, you proceed with caution. A conditional works the same way, checking one thing at a time from the top.

The signpost analogy has a limit, though. A real signpost can point in several directions at once, but if / elif / else picks only one path. The moment it finds the first path that fits, it doesn't even look at the rest.

The basic shape: turning a score into a grade

score = 78
if score >= 90:
    grade = "A"
elif score >= 70:
    grade = "B"
else:
    grade = "C"
print(grade)
print(score >= 70 and score < 90)
B
True

Here's how to read it:

  • The line after if condition: is indented (usually 4 spaces). Only indented lines belong to that condition.
  • elif means "if the previous condition was false, check this one next."
  • else means "if all the conditions above were false." It takes no condition.
  • and is true when both sides are true. or is true when at least one side is true.

Here are the comparison operators you'll see most:

OperatorMeaningExample
==Equal toanswer == "yes"
!=Not equal tostatus != "done"
>, <Greater than, less thanage > 19
>=, <=Greater than or equal to, less than or equal toscore >= 90

Common trap: does elif order matter?

Let's flip the order and plug in a score of 95.

score = 95
if score >= 70:
    grade = "B"
elif score >= 90:
    grade = "A"
print(grade)
B

The score is 95, but you get a B. The first condition, score >= 70, is already true, so Python stops there and never even checks score >= 90. No error message appears either. Bugs like this, where the code runs fine but the result is wrong, are easy to miss when reviewing AI code.

The fix comes down to one rule: put the narrower (stricter) condition first. "90 or above" is narrower than "70 or above," so it goes on top.

Common trap 2: mixing up = and ==

if x = 3: doesn't run. It raises a SyntaxError. = is the symbol for attaching a name (Part 2), and comparisons need ==. Don't panic when you see the error. Start by checking whether the condition has just a single =.

How to check conditionals the AI wrote

Most problems in a conditional show up when you test boundary values: the cutoff numbers and the values right next to them.

  • If the cutoffs are 90 and 70, try 90, 89, 70, 69.
  • Write down the result you expect for each value first, then compare it with the actual result.
  • Whether it's >= or > changes the result right at the boundary. If 90 should be an A, >= 90 is correct.

You can ask the AI something like this:

  • "Add boundary value tests to this conditional. Print the result for 90, 89, 70, and 69."
  • "Conditions are checked from the top, so make sure the narrower conditions come first."

Check yourself

  1. In the trap code, what is grade when score = 95? → B. The first condition is true, so it stops. elif is only checked when the if before it is false.
  2. Why is if x = 3: an error? → Because = is assignment, and comparison uses ==.

Try it yourself

Store an age in a variable and print "Child," "Teen," or "Adult." If you set the cutoffs as under 13 for a child and under 19 for a teen, try 12, 13, 18, and 19 to confirm each one comes out as intended. If you get stuck, make a table of whether >= or > is right at each boundary.

age = 13
if age < 13:
    print("Child")
elif age < 19:
    print("Teen")
else:
    print("Adult")
Teen

Previous and next

The full series list is at the end of Part 10.

Back to list