Python - 11

Python - 11: Else & Elif Statements

elif and else let your program choose between several possible outcomes.

Your challenge: Check a score and a weather value, then display different messages depending on which condition matches.

✦ Python Editor
▶ Output

Use elif for another condition

Python checks the if condition first. If it is false, it can check an elif condition next.

For example: elif score >= 70:

Use else as the fallback

else runs when none of the earlier conditions are true.

Only one branch in an if / elif / else chain will run.

Order matters

Check the highest score first. If you checked score >= 70 before score >= 90, a score of 95 would match the first condition too early.

Try it: Change score to 95, 75, and 50 to test all three outcomes.

Good Python habit: Arrange related conditions from most specific or highest priority to least specific.

👀 Show Example Solution
score = 85

if score >= 90:
    print("Amazing job!")

elif score >= 70:
    print("You passed!")

else:
    print("Keep practicing!")

weather = "rainy"

if weather == "sunny":
    print("Go outside!")

elif weather == "rainy":
    print("Bring an umbrella!")

else:
    print("Check the weather again.")

Python checks each condition from top to bottom and stops when it finds the first matching branch.