Control Flow
Branching with if / elif / else
if / elif / else let a program choose between paths. Python checks each condition top to bottom and runs the FIRST block whose condition is true, then skips the rest.
if temperature >= 30:
label = "hot"
elif temperature >= 15:
label = "mild"
else:
label = "cold"Order matters: check the highest / most specific condition first. Because the first true branch wins, an elif only runs when every condition above it was false.
Your turn
Complete grade_for(score) with if / elif / else: return "A" for 90 or above, "B" for 70 or above, "C" for 50 or above, otherwise "F".
Lesson complete. Nice work.
Code · runs in your browser
Output
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.