Beginner · 10 min
More than two choices
Handle several cases with if, elif, and else.
Check conditions in order
elif means another condition is checked if the earlier one was false. Python runs the first matching block, then skips the rest of the chain.
score = 75
if score >= 80:
print("Excellent")
elif score >= 50:
print("Pass")
else:
print("Try again")Output
Pass
Put the narrower case first
Order matters when conditions overlap. Check the highest threshold first here, so a high score does not get the lower label.
score = 92
if score >= 80:
print("Excellent")
elif score >= 50:
print("Pass")Output
Excellent
More than two choices
- Follow the conditions in their written order.
- Test a value exactly on a threshold.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Label a score
Keep score as 82. Set label to High for scores of at least 80, Pass for at least 50, and Retry otherwise. Display label.
Start with the highest threshold, then use elif and else.