Lesson 2.3

Admin notes

  • Unit 1 Quiz and Test

Looking ahead

  • PE 1 Module 2
  • Unit 2 Quiz (if-elif-else and while loops)

If–Elif–Else Statements

Why do we need elif?

  • if gives us one choice
  • if … else gives us two choices
  • What if we want 3 or more?

Syntax of elif

if condition1:
    # runs if condition1 is True
elif condition2:
    # runs if condition2 is True
else:
    # runs if none above are True

Example

score = int(input("Enter your test score: "))
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
else:
    print("Below C")

Important rules

  • Python checks conditions top to bottom
  • As soon as one condition is True, it runs that block and skips the rest
  • Indentation rules are the same as before

Classroom Question

What does this print if x = 15?

x = 15
if x > 20:
    print("Big")
elif x > 10:
    print("Medium")
else:
    print("Small")

Practice Challenge

Ask the user for the temperature:

  • If 85 or above → "It's hot"
  • If 60 or above → "It's nice"
  • Else → "It's cold"