- Home
- Lessons
- KS3 Computing
- Python: making decisions with if
Python: making decisions with if
🎬 The doodle video for this lesson is coming soon. Subscribe on YouTube to see it first.
Programs become useful when they can make decisions. In Python, if, elif and else choose which code runs.
Comparison operators
if, elif, else
score = 65
if score >= 70:
print("Distinction")
elif score >= 50:
print("Pass")
else:
print("Try again")
Python checks each condition from the top. The first one that is True runs, and the rest are skipped.
else runs only if nothing above it was True.
The indentation (the spaces at the start of a line) shows which lines belong to each part.
if score >= 70:
print("Distinction")
elif score >= 50:
print("Pass")
else:
print("Try again")
Python checks each condition from the top. The first one that is True runs, and the rest are skipped.
else runs only if nothing above it was True.
The indentation (the spaces at the start of a line) shows which lines belong to each part.
Common mistakes
Forgetting the colon : at the end of the if line.
Forgetting to indent the code inside.
Putting conditions in the wrong order: check the highest score first, or a high score would only get Pass.
Forgetting to indent the code inside.
Putting conditions in the wrong order: check the highest score first, or a high score would only get Pass.
What does the program print when score = 65?
score = 65
if score >= 70:
print("Distinction")
elif score >= 50:
print("Pass")
else:
print("Try again")
- Is 65 >= 70? No.
- Is 65 >= 50? Yes, so print Pass and skip the rest.
Answer: Pass
if checks a condition; elif checks another if the first was False; else catches everything left. Use == to compare, end the line with a colon and indent the code inside.
The interactive lesson includes the diagrams for this topic.
Check you have got it
Answer 6 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.