The elif Statement

STEP 3 — elif (a.k.a. Your Game’s “Wait, Actually…” Button)

Step 3 is here, and this is the moment your game goes from “I can make one decision” to

“I can make MULTIPLE decisions like a full-blown thinking machine.”

Let’s unleash the chaos.

Alright coder, you’ve met:

  • if — the confident decision
  • else — the backup plan

But what if your game needs more than two choices?

Enter elif, the middle child of conditions.

Not the first choice. Not the last resort. Just… perfectly in between.


1. What Is elif?

elif stands for else if.

It lets your game check another condition when the first one wasn’t true.

It’s like your code saying:

  • “If health is low, warn the player.”
  • “ELIF health is medium, give advice.”
  • “ELSE… they’re fine, chill.”

Your game now has layers.


2. Basic Structure

(This code block will throw an error if you try to run it because you have not specified the conditions. See conditions specified in Example 3 below.)

if condition1:
    # do this
elif condition2:
    # do this instead
else:
    # final option

This is your game making decisions like a responsible adult.

Finally.


3. Example: Health Status System

health = 60

if health < 30:
    print("Critical! Heal now!")
elif health < 70:
    print("You're okay, but stay alert.")
else:
    print("You're in great shape!")

Your game now gives medical advice. Probably more accurate than WebMD.

Try changing the health value and click Run

4. Example: Enemy Behavior

enemy = "dragon"

if enemy == "slime":
    print("Easy fight!")
elif enemy == "goblin":
    print("Watch out!")
elif enemy == "dragon":
    print("RUN!")
else:
    print("Unknown creature...")

Your game now reacts appropriately to dragons. As in: panic.

Modify and Run

Practice (Your Turn!)

Mini-Challenge

Write conditions that:

  • print "Weak attack" if damage < 10
  • print "Normal attack" if damage < 20
  • otherwise print "Strong attack!"
Modify and Run

Boom.

Your game now judges attacks like a picky coach.