Many Paths
Hoppy comes to a three-way intersection. Not everything is just "this or that". Sometimes there are many options.
Python uses elif (short for "else if") to handle multiple choices.
The Elif Spell
color = "red"
if color == "blue":
print("Sky")
elif color == "green":
print("Grass")
elif color == "red":
print("Fire")
else:
print("Unknown color")
Python checks them one by one. As soon as it finds a match, it stops checking the rest.
Your Mission
1
The Sign
Create a variable path and set it to "mountain".
2
Forest Path
Check if path is "forest". If so, print "Trees".
3
Mountain Path
Use elif to check if path is "mountain". If so, print "Rocks".
4
Lost
Use else to print "Lost" if neither matches.
Suggested SolutionClick to expandClick to collapse
Here is how to navigate:
path = "mountain"
if path == "forest":
print("Trees")
elif path == "mountain":
print("Rocks")
else:
print("Lost")Advanced TipsWant more? Click to expandClick to collapse
Order matters! Python takes the first True path it finds.
score = 95
if score > 90:
print("A")
elif score > 80:
print("B")
# If we swapped these, 95 would print "B" if we checked > 80 first!pymain.py
Loading...
Terminal
Terminal
Ready to run...