if ... else: Plan B ๐ฆ๏ธ
if does something when the answer is True. But what about when it's False?
That's where else comes in โ it's your backup plan.
age = 8
if age >= 13:
print("Welcome to the teen zone!")
else:
print("Come back in a few years ๐")
Exactly one of those two messages prints. Never both, never neither. Python picks a path and takes it.
(>= means "greater than or equal to", by the way.)
Booleans work great with if all by themselves:
raining = True
if raining:
print("Umbrella time โ")
if raining: is just a shorter way of saying if raining == True:.
Your mission
- Make a variable
rainingand set it toTrueorFalse(no quotes!) - If it's raining, print a rainy-day message
- Use
elseto print a totally different sunny-day message
Your code
Press โถ Run to see what your code does!