Python 'elif'

Can you build an else statement from scratch?

score = 111
if score < 100:
   print(“Almost there!”)
else :
   print(“You won!”)

You won!

Perfect! If score isn’t less than 100, it must be 100 or more.

Else needs to be the last part of an if statement. Can you get this code to make sense?

if 1 < 100:
   print(“Less than 100!”)
else:
   print(“Bigger than100!”
Less than 100!

Yes, that’s the order that makes sense!

There’s one more thing we can add to our if statement. elif allows us to add additional conditions beyond the initial condition.

hour = 12
if hour < 12:
   print(“Good morning”)
elif hour < 17:
   print(“Good afternoon”)

Good afternoon

Great! Now the computer displays Good afternoon when hour isn’t less than 12 but is less than 17.