Class 9 · Unit 5: Python · Lesson 5.4 · 40 min
Making decisions — if, elif, else
A big apple with scab on it is not an A. One rule, two conditions — and both must be true.
Today you will: Ask True/False questions in Python · Choose one branch with if, elif, else · Join two conditions with and
Story
Bilal has written this rule three times in his life.
In Class 6 he drew it as a decision tree: is the apple bigger than 8 cm? then A grade. In Class 7 he built it in a spreadsheet, as a formula with IF in it. Both worked.
Today his uncle asks for something the tree could not do. "A grade is not only size," he says. "A big apple with scab on it is not an A. It is not even a B."
One rule, two conditions, and they both have to be true at once.
Watch
if, elif, else — — scan code 9.5.4 in the printed book.
Warm up your fingers
Today's symbols are the colon : and the comparison signs > < =.
:is Shift + ; — the key beside L. Everyifline in Python ends with it.==is two equals signs typed together, with no space between them.- typing.com: continue the symbols lessons.
- Then type these two lines twice, with no mistakes:
if size > 8:elif grade == "B":
The colon is the single most forgotten character in Python. Your fingers should learn it today.
On the laptop
You need: Thonny · save as 04-grader.py
Mission 1: True or False? (5 min)
- ☐1
Guess True or False for each first. Then type them into the shell. The last two catch most people.
8 > 6 8 >= 8 8 != 6 "Ambri" == "Ambri" "ambri" == "Ambri"=stores.==compares.
Mission 2: One question, three answers (7 min)
- ☐2
In the editor:
size = float(input("Apple size in cm: ")) if size > 8: print("A grade") elif size > 6: print("B grade") else: print("C grade") - ☐3
Predict the grade for 9, 7, 5 and 8. Run all four. Is 8 what the uncle would want? A value on the border is an edge case.
Mission 3: Two conditions (4 min)
- ☐4
Add
spots = input("Any scab spots? y or n: ")at the top, and change the first test toif size > 8 and spots == "n":. Test 9 withy, then withn.
Mission 4: Break it (2 min)
- ☐5
Delete the
:afterif→SyntaxError: expected ':'. Put it back, delete the four spaces beforeprint("A grade")→IndentationError. Fix both and save.
Stuck?
Always C grade? Text is exact: type a lowercase n. IndentationError that won't go? Delete
the line's leading space and press the space bar four times. SyntaxError on the if line?
You wrote = for ==.
Finished early?
Add a Reject grade for under 4 cm, then make scab or under 4 cm send it to Reject, using
or. Test every outcome.
No laptop today?
Trace the two-condition grader for (9, "n"), (9, "y") and (5, "n"): which branch runs,
what prints? Then: why can a 9 cm apple with spots never reach else?
Now you know
- A condition is a question Python answers
TrueorFalse, using><>=<===!=. - = stores. == compares. This is the most common mistake in the language.
if…elif…elsechooses one branch. The first true condition wins, and the rest are skipped.- Indentation is not decoration. Four spaces tell Python which lines belong inside.
andneeds both sides true;orneeds one;notflips it.- Two errors you will meet today:
SyntaxError: expected ':'(a missing colon) andIndentationError(a missing or uneven indent).
Debate it
Your program decides an apple's grade, and the grade decides what the farmer is paid.
The rule in the code — size > 8 and spots == "n" — was written by someone. Who should
decide where the line sits: the trader who buys, the grower who sells, or the person who
writes the program? What should happen to an apple at exactly 8 cm, and who should be
told that the rule works that way?
Check yourself — practice, not a test
1. What does == do?
- ○ Puts a value into a variable
- ○ Asks whether two values are equal
- ○ Adds two numbers
- ○ Ends a line
2. What does this print? python marks = 7 if marks > 8: print("High") elif marks > 5: print("Middle") else: print("Low")
- ○ High
- ○ Middle
- ○ Low
- ○ Middle and Low
3. Find the bug. python if size > 8 print("A grade")
- ○ print should be indented further
- ○ The colon is missing after the condition
- ○ size must be in quotes
- ○ if should be elif
4. If the if condition is True, Python also checks the elif below it.
- ○ True
- ○ False
5. A and B is True only when ______ sides are True.
6. Match each operator to its meaning:
!= · >= · ==
Match with: not equal to · equal to · greater than or equal to7. Put these lines in the order they must appear, so an apple is graded correctly:
- 1. print("A grade")
- 2. if size > 8:
- 3. else:
- 4. print("B grade")
- 5. print("C grade")
- 6. size = float(input("Apple size in cm: "))
- 7. elif size > 6:
Remember
= stores, == compares. Colon at the end, four spaces underneath.