Unit 2 Β· Lesson 3

elif Chains

Life isn't just yes or no β€” sometimes there are three, four, or ten possibilities. elif lets your code handle them all.

Write multi-way if/elif/else chains
Understand why order matters
Use a catch-all else
Build real classifiers and converters

Beyond Two Choices

In Lesson 2.2, you used if/else to choose between two paths. But what about letter grades? A score of 95 is an A, 85 is a B, 75 is a C… You need more than two branches.

That's where elif (short for "else if") comes in:

score = 85 if score >= 90: grade = "A" elif score >= 80: grade = "B" elif score >= 70: grade = "C" elif score >= 60: grade = "D" else: grade = "F" print(f"Score: {score} β†’ Grade: {grade}") # Score: 85 β†’ Grade: B

Here's how Python evaluates this β€” it checks from top to bottom and stops at the first True condition:

if score >= 90 β†’ "A" checked first β€” 85 < 90, skip
elif score >= 80 β†’ "B" 85 >= 80 βœ“ β€” stops here!
elif score >= 70 β†’ "C" never checked
elif score >= 60 β†’ "D" never checked
else β†’ "F" never reached
Key insight: Python only runs one branch in an if/elif/else chain. The moment it finds a True condition, it runs that block and skips everything below β€” even if other conditions would also be True.

Why Order Matters

Because Python stops at the first True condition, the order of your elif checks is crucial. Watch what happens if we reverse the grade check:

score = 95 # ❌ WRONG ORDER β€” always gives "D" for any passing score! if score >= 60: grade = "D" # 95 >= 60 is True β€” stops here! elif score >= 70: grade = "C" # never reached elif score >= 80: grade = "B" # never reached elif score >= 90: grade = "A" # never reached
⚠️ Rule of thumb: When using >= comparisons, check the highest value first and work down. When using <=, check the lowest first and work up. Think: check the most specific/restrictive condition first.

The Safety Net: else

The else at the end is your catch-all. It handles anything that didn't match any condition above it. It's optional, but usually a good idea:

day = "Saturday" if day == "Monday": mood = "😩" elif day == "Friday": mood = "πŸŽ‰" elif day == "Saturday" or day == "Sunday": mood = "😎" else: mood = "😐" # Tue, Wed, Thu
Pro tip: You can have as many elif branches as you need β€” there's no limit! Just remember: exactly one branch will run.
Practice Time

Challenges

πŸ…°οΈ

Challenge 1: Letter Grade

Guided

Build a grade converter using an if/elif/else chain.

Instructions: Set the variable grade based on the score:
β€’ 90+ β†’ "A"
β€’ 80–89 β†’ "B"
β€’ 70–79 β†’ "C"
β€’ 60–69 β†’ "D"
β€’ Below 60 β†’ "F"
Hint 1: Start with if score >= 90: and work your way down. Each elif checks the next threshold.
Hint 2: You don't need to check both ends of a range! Since Python stops at the first True, elif score >= 80: only runs if the score is already less than 90.
Hint 3: if score >= 90: β†’ "A", elif score >= 80: β†’ "B", etc. End with else: β†’ "F".
πŸ‚

Challenge 2: Season Finder

Guided

Convert a month number to its season.

Instructions: Given a month number (1–12), set season:
β€’ Months 3, 4, 5 β†’ "Spring"
β€’ Months 6, 7, 8 β†’ "Summer"
β€’ Months 9, 10, 11 β†’ "Fall"
β€’ Months 12, 1, 2 β†’ "Winter"

Hint: You can check if a month is "in" a group using or, like month == 3 or month == 4 or month == 5
Hint 1: This is an elif chain where each condition checks 3 months. Start with Spring!
Hint 2: if month == 3 or month == 4 or month == 5: sets season to "Spring". Do the same for Summer and Fall, then use else: for Winter.
Hint 3: Winter is the trickiest because it wraps (12, 1, 2). Using else: catches all three!
βš•οΈ

Challenge 3: BMI Category

Solo

Calculate BMI and classify it into a health category.

Instructions:
1. Calculate bmi using the formula: weight / (height ** 2)
2. Set category based on the BMI value:
β€’ Below 18.5 β†’ "Underweight"
β€’ 18.5 to 24.9 β†’ "Normal"
β€’ 25.0 to 29.9 β†’ "Overweight"
β€’ 30.0 and above β†’ "Obese"
Hint 1: First calculate: bmi = weight / (height ** 2). For 70kg and 1.75m, that's about 22.9.
Hint 2: Start your chain with the lowest threshold: if bmi < 18.5: β†’ "Underweight", then work up.
Hint 3: elif bmi < 25: β†’ "Normal", elif bmi < 30: β†’ "Overweight", else: β†’ "Obese".
✊

Challenge 4: Rock Paper Scissors

Stretch

Determine the winner of a Rock Paper Scissors game using elif chains.

Instructions: Given player1 and player2 choices ("rock", "paper", or "scissors"), set result to:
β€’ "tie" β€” if both chose the same thing
β€’ "player1" β€” if player 1 wins (rock beats scissors, scissors beats paper, paper beats rock)
β€’ "player2" β€” if player 2 wins

There are multiple valid approaches. You could check for a tie first, then check all win conditions for player 1, then use else for player 2.
Hint 1: Start easy β€” check for a tie first: if player1 == player2:
Hint 2: Player 1 wins in exactly 3 cases. You can check all three with or:
elif (player1 == "rock" and player2 == "scissors") or ...
Hint 3: After checking tie and all player1 wins, everything left must be a player2 win β€” use else:!