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
Rahat 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.
Learn it
A program becomes useful when it can decide. A condition is a question Python answers True or
False, and if / elif / else choose one path based on it. You have written this logic before,
as a decision tree in Class 6 and an IF formula in Class 7; now you write it in Python, where
indentation decides which lines belong to which choice, and and lets one rule need two things
at once.
Words to know
- Condition a question with a
TrueorFalseanswer, likesize > 8. - Comparison operators
><>=<===(equal to)!=(not equal to). - = vs ==
=stores a value;==compares two values. Mixing them up is the commonest mistake in Python. - if / elif / else
ifasks the first question;elif("else if") asks the next;elsecatches everything left. Only the first true branch runs. - Indentation the four spaces that tell Python which lines are inside a choice.
- and / or / not
andneeds both sides true;orneeds one;notflips True and False. - Edge case a value right on the border, like exactly 8 when the rule says
> 8.
Step by step: grading exam marks
marks = int(input("Marks out of 100: "))
present = input("Attended the practical? y or n: ")
if marks >= 75 and present == "y":
print("Distinction")
elif marks >= 40:
print("Pass")
else:
print("Try again")
- 82 and
y: both sides ofandare true → Distinction. The other branches are skipped. - 82 and
n: theandfails, so Python checkselif marks >= 40→ Pass. - 30: neither is true →
else→ Try again. - Edge case, exactly 75 and
y:>=includes 75 → Distinction. With>it would not, so choose carefully. - Two errors to expect: a missing
:givesSyntaxError: expected ':'; missing spaces giveIndentationError.
Careful: indentation is not style in Python; it is grammar. The indented lines are the ones
that belong to the choice above them. And text comparisons are exact: "N" is not "n", so
tell the user which letter to type.
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?
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.
Typing practice
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.