Why elif Order Changes the Answer in Python

Why elif's ORDER can make a branch impossible to reach — and when separate ifs are the right call instead.

  • Lesson 5 of 5
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

Order and overlap

  1. The dragon: You know elif stops at the first match. Here's a trap that causes: writing the conditions in the wrong ORDER.

  2. The dragon: score = 95. This chain checks the LOOSE condition first — watch what happens to the branch below it.

    example.pyPython 3
    score = 95if score > 50:    print("Good")elif score > 80:    print("Excellent")
    OutputGood
  3. The dragon: Excellent never prints — and it NEVER CAN, for any score. Every value bigger than 80 is also bigger than 50, so the first check always wins first.

  4. The dragon: Fix it by putting the NARROWEST, most specific condition first. Same two conditions, opposite order.

    example.pyPython 3
    score = 95if score > 80:    print("Excellent")elif score > 50:    print("Good")
    OutputExcellent
  5. The dragon: Same rule, now with score = 60 — it doesn't satisfy > 80, so it falls through to check > 50 instead.

  6. The dragon: elif only checks each condition if every one above it was False — so the order you write them in changes what's actually reachable.

    example.pyPython 3
    score = 60if score > 80:    print("Excellent")elif score > 50:    print("Good")
    OutputGood
  7. The dragon: Now a completely different question: does everything with more than one condition NEED elif? No.

  8. The dragon: burns_survived = 3, rooms_found = 5 — two UNRELATED facts. Both badges should be earned. elif here would be the bug.

    example.pyPython 3
    burns_survived = 3rooms_found = 5if burns_survived >= 3:    print("Fireproof")elif rooms_found >= 5:    print("Explorer")
    OutputFireproof
  9. The dragon: Explorer never printed — elif skipped it because Fireproof's condition already matched. But these facts don't compete, so that's wrong.

  10. The dragon: Two SEPARATE ifs fix it — each one checked on its own, so both badges can be earned in the same run.

    example.pyPython 3
    burns_survived = 3rooms_found = 5if burns_survived >= 3:    print("Fireproof")if rooms_found >= 5:    print("Explorer")
    OutputFireproof Explorer
  11. The dragon: The rule: elif for tiers of ONE thing, where only one should ever win. Separate ifs for independent facts, where more than one can be true.

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 2

    score = 95. What does this print? if score > 50: print("Good") elif score > 80: print("Excellent")

    question.pyPython 3
    score = 95if score > 50:    print("Good")elif score > 80:    print("Excellent")
    • GoodCorrect
    • Excellent — 95 is a better match for that condition
    • Both Good and Excellent

    Why: Good — and Excellent can NEVER print with this ordering, for ANY score. Every number that satisfies score > 80 also satisfies score > 50, and score > 50 is checked first. Writing the narrower, more specific condition FIRST is what makes a branch reachable at all.

  2. Question 2 of 2

    burns_survived = 3, rooms_found = 5 — the adventurer earned BOTH badges. Which code correctly awards both?

    question.pyPython 3
    if burns_survived >= 3:    print("Fireproof")??? rooms_found >= 5:    print("Explorer")
    • Two separate if statementsCorrect
    • if / elif — elif is always the safer choice once there's more than one condition

    Why: Two separate ifs — because Fireproof and Explorer are independent facts, not competing tiers. elif would silently drop Explorer the moment Fireproof matched, the same order-sensitivity trap as an unreachable elif branch, just from the opposite direction: chaining things that were never supposed to compete.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    points = 95. Give exactly one reward tier: 90 or more is Platinum, 70 or more (but under 90) is Gold, anything else is Silver. Write the chain with the NARROWEST, highest condition FIRST — if you check the loose condition first, the tight one can never be reached.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.