Python elif and else: Chains of Decisions Explained

Chains of decisions — and why the ORDER and the KIND of check both matter.

  • Lesson 3 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

elif and else

  1. The dragon: You know if. Now let's chain several questions together, and add a catch-all for everything else.

  2. The dragon: elif means 'otherwise, if THIS is true' — it only gets checked if everything above it was False.

    example.pyPython 3
    gold = 150if gold >= 100:    print("So generous!")elif gold > 0:    print("Thank you.")
    OutputSo generous!
  3. The dragon: else is the catch-all at the end — no condition of its own, it just catches whatever nothing else matched.

    example.pyPython 3
    gold = 0if gold >= 100:    print("So generous!")elif gold > 0:    print("Thank you.")else:    print("...")
    Output...
  4. The dragon: Here's the important part. An elif chain STOPS at the first True — even if a later condition would ALSO be true.

    example.pyPython 3
    strength = 90if strength > 80:    print("Legendary!")elif strength > 50:    print("Strong.")
    OutputLegendary!
  5. The dragon: 90 is more than 50 too — but you never see 'Strong', because elif already stopped at Legendary. Only one branch ever runs.

  6. The dragon: Compare that to two SEPARATE ifs — those don't stop each other. Both can fire on the same number.

    example.pyPython 3
    strength = 90if strength > 80:    print("Legendary!")if strength > 50:    print("Strong.")
    OutputLegendary! Strong.

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

    strength = 90. Which of these will print BOTH Legendary and Strong?

    • Two separate if statementsCorrect
    • if / elif
    • Neither — they always do the same thing

    Why: Two separate ifs check independently, so both can fire on the same value: Legendary AND Strong. if/elif is a CHAIN — it stops at the first match, so only Legendary prints. They agree on most values, but not on this one, which is exactly why the difference matters.

  2. Question 2 of 2

    gold = 0. What does the else branch need for its OWN condition to run?

    question.pyPython 3
    if gold >= 100:    print("So generous!")elif gold > 0:    print("Thank you.")else:    print("...")
    • Nothing — else has no condition of its ownCorrect
    • else needs its own comparison, like else gold == 0:
    • else only works if there was exactly one elif before it

    Why: else takes no condition of its own — writing else gold == 0: is actually a Python error. It runs automatically whenever nothing above it matched.

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

    Three caves. After one step, the dragon stands on a lever and looks ahead. A knight? Breathe fire. A closed gate? Pull the lever. Nothing at all? Say "All clear!". Use if, elif and else so exactly one of those happens in each cave — then walk on to the gold.

  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.