elif Chains
Life isn't just yes or no β sometimes there are three, four, or ten possibilities. elif lets your code handle them all.
if/elif/else chainselseBeyond 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:
Here's how Python evaluates this β it checks from top to bottom and stops at the first True condition:
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:
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:
Challenges
Challenge 1: Letter Grade
Build a grade converter using an if/elif/else chain.
β’ 90+ β "A"
β’ 80β89 β "B"
β’ 70β79 β "C"
β’ 60β69 β "D"
β’ Below 60 β "F"
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
Convert a month number to its 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 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
Calculate BMI and classify it into a health category.
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"
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
Determine the winner of a Rock Paper Scissors game using elif chains.
β’ "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.
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:!