elif: More Than Two Paths ๐ฅ
Sometimes there are more than two possibilities. elif (short for
"else if") lets you chain as many checks as you want:
grade = 85
if grade >= 90:
print("Grade: A โญ")
elif grade >= 80:
print("Grade: B")
elif grade >= 70:
print("Grade: C")
else:
print("Let's practice some more!")
Here's the important part: Python reads the chain top to bottom, stops at the first check that's True, runs that one, and ignores everything below it. Only one message ever prints.
You can have as many elifs as you like. The else at the end catches
everything that didn't match.
Your mission
Build a race medal announcer!
- Make a variable
placewith the number you finished in - If
placeis 1, print a gold medal message ๐ฅ - Use
eliffor 2nd place ๐ฅ andeliffor 3rd place ๐ฅ - Finish with an
elsefor everyone else
Your code
Press โถ Run to see what your code does!