Using else and elif for Multiple Outcomes

STEP 2 β€” Using else and elif for Multiple Outcomes

In Step 1, you learned how an if statement checks a condition and runs code only when that condition is True.

But games rarely have just one possible outcome.

Players make choices, events unfold, and your program needs to respond in different ways depending on the situation.

This is where else and elif come in.


1. The else Statement

else is used when you want something to happen if the condition is False.

Structure:

if condition:
    # runs when condition is True
else:
    # runs when condition is False

Example: Checking if the player has enough coins

coins = 8

if coins >= 10:
    print("You can buy the sword.")
else:
    print("You do not have enough coins.")
Click Run to execute.
  • If the player has 10 or more coins β†’ they can buy the sword.
  • Otherwise β†’ the game gives a different response.

2. The elif Statement

elif (short for β€œelse if”) lets you check additional conditions when the first one is not True.

Structure:

if condition1:
    # first option
elif condition2:
    # second option
else:
    # fallback option

Example: Health Status System

health = 45

if health > 70:
    print("Health is high.")
elif health > 30:
    print("Health is moderate.")
else:
    print("Health is low.")
Click Run to execute.

This creates three different outcomes based on the player’s health.


3. Why This Matters in Games

if, elif, and else allow your game to:

  • react to different player states
  • create branching story paths
  • handle multiple difficulty levels
  • determine enemy behavior
  • manage item requirements

This is the foundation of interactive gameplay.


Practice (Your Turn!)

Mini-Challenge

Create a variable xp.

Write conditions that print:

  • “Level up!” if xp is 100 or more
  • “Keep going!” if xp is between 50 and 99
  • “Just getting started.” if xp is below 50
Write code and Run.