Decisions in code
About this lesson: Decisions in code
In short
In Python, if checks a condition and else handles everything else. The colon and the indent (4 spaces) show which lines belong to each part. Change the age.

What you'll explore
- if and else: A two-way choice in Python
- In Python, if checks a condition and else handles everything else. The colon and the indent (4 spaces) show which lines belong to each part.
- elif: More than two choices
- elif means ‘else if’. Python checks each condition from the top and runs only the first one that is True.
- Order matters: Put the strictest test first
- Because Python stops at the first True condition, the order can cause a bug. Set the score to 85 and look at the output, then swap the order.
- Check yourself: Indent it
- Ben writes if lives == 0: on one line, and print("Game over") on the next line, starting right at the left edge. Python shows an error.
Also in this lesson
- Common mix-ups
- Python patterns
- Your own menu
Words to know
- if
- Runs code only when a condition is True.
- elif
- Checks another condition if the earlier ones were False.
- else
- Runs when no condition was True.
- indentation
- Spaces at the start of a line that show which block it belongs to.
- ==
- Compares two values for equality.
Sources
- Python documentation: if statements