Python · Stage 3 – Making Decisions · Lesson 21 of 50

else and elif

Choose one path when a condition is false or test another condition.

is_raining = False
if is_raining:
    print("Take an umbrella")
else:
    print("Enjoy the sun")
Line-by-line explanation
  1. The first line stores False.
  2. The if condition checks that value.
  3. Its indented line is skipped.
  4. else provides the other path.
  5. The final message is displayed.
score = 70
if score >= 90:
    print("Gold")
elif score >= 60:
    print("Silver")
else:
    print("Bronze")
Line-by-line explanation
  1. The score is stored.
  2. The first condition is False.
  3. The elif condition is True, so Silver is displayed.
  4. Python skips the remaining else block.

Tiny Practice

Display “Open” when is_open is True and “Closed” otherwise.

Starter template

is_open = False
if is_open:
    print("Open")
CHANGE_ME
Line-by-line explanation
  1. The if path is complete.
  2. You need the otherwise path.
Hint
  1. Use else.
  2. Add a colon.
  3. Indent the Closed message.
Show Solution
is_open = False
if is_open:
    print("Open")
else:
    print("Closed")
Line-by-line explanation
  1. False skips the first message.
  2. The else path runs.
  3. The output is Closed.