Beginner · 10 min
Making decisions
Choose between two actions with if and else.
Run code when a condition is true
An if statement checks a condition. End its first line with a colon and indent the code below it. Use four spaces consistently for this lesson.
temperature = 12
if temperature < 15:
print("Take a jacket")Output
Take a jacket
Choose the other action
An else block runs when the if condition is false. Keep else aligned with if. Only one of these two blocks runs.
stock = 0
if stock > 0:
print("Available")
else:
print("Sold out")Output
Sold out
Making decisions
- Mark the block selected by each condition.
- Check the colons and indentation.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Choose a delivery message
Keep total as 25. If it is at least 20, store Free delivery in message; otherwise store Delivery fee. Display message.
Write an if and an else, then print the message after both blocks.