Concept
Multi-Choice Branching (elif)
Short for "else if", `elif` lets you test multiple options in a row:
python
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "F"# 46. Multi-Choice Branching (elif) Short for "else if", `elif` lets you test multiple options in a row: ```python score = 85 if score >= 90: grade = "A" elif score >= 80: grade = "B" else: grade = "F" ``` The moment Python finds a matching branch, it executes it and skips all the other branches below it.
Checkpoint
In an `if/elif/else` chain, what happens when the first `elif` matches?
Practice
Write an `if/elif/else` checking `speed = 75`. If `speed > 100`, print `"Too fast"`. If `speed > 60`, print `"Cruising"`. Otherwise print `"Slow"`.
01
Tap code blocks below to assemble
Available Blocks