AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Python makes the choice

Week 6 · 1.4 Conditional statements in text-based programming · 3 lessons
2

Else and the three questions

Computing 8: Topic 1.4, pp. 70–105
GoalWrite a Python if ... else, plan an if statement in three steps and fix a missing colon or indent.
New words
else · the part of an if that runs when the condition is false; it has no condition and ends with a colonsyntax error · a mistake in the rules of the language, so the program will not startIndentationError · the message Python gives when a line is not indented as it should be
Explanation

An if ... else statement gives two paths. Lines indented under else: run in every other case. The word else has no condition, and it ends with a colon too. It is written at the same level as the if it belongs to, and its lines are indented. Before typing, plan the statement in three steps. Step one: what is the input and what is stored? Step two: what is the condition? Step three: what happens when it is true, and what happens when it is false? Python checks the rules before it runs anything. A missing colon or a missing indent is a syntax error, so the program does not start. A mistake like that is different from a logic error, where the program runs but gives the wrong answer.

StepQuestionAnswer for the battery program
1What is the input?percent, a whole number
2What is the condition?percent < 20
3True: what happens? False: what happens?print Charge soon; print Battery fine
Worked examples
Python
1percent = int(input("Battery? "))
2if percent < 20:
3    print("Charge soon")
4else:
5    print("Battery fine")
Output
Charge soon
A phone battery checker. With 15 typed, the condition is true, so Charge soon is printed. With 60 typed, the else lines run and Battery fine is printed.
Python
1percent = int(input("Battery? "))
2if percent < 20:
3    print("Charge soon")
4print("Checked")
Output
Charge soon
Checked
Indentation shows which lines are inside the if. The last line is not indented, so it runs every time. With 15 typed, the output is two lines. With 60 typed, only Checked is printed.
MistakeLast line of the message
colon missing after the conditionSyntaxError: expected ':'
print not indentedIndentationError: expected an indented block after 'if' statement on line 2
Two mistakes that stop a program from starting. Python shows the type of error on its last line. A missing colon gives the first message; a missing indent gives the second.
Class activity

Three-question planner (no computer). Choose a situation, such as ‘a school gate bell rings if it is after 8 o’clock’ or ‘a pet needs food if its bowl is nearly empty’. Draw a table with the three questions: input, condition, and what happens when true and when false. Fill it in. Write the if ... else program on paper, with colons and indentation. Ask a partner to find one deliberate mistake that you add (a missing colon or indent). Swap roles. If you have a computer or tablet, type a program with and without the mistake and read the last line of the message.

Practice
1
Rewrite this pseudocode in Python.
Pseudocode
1INPUT kg
2IF kg > 20 THEN
3    OUTPUT "Extra bag fee"
4ELSE
5    OUTPUT "No fee"
6ENDIF
2
Python does not start this program. Name the type of error, say which line is wrong and give the corrected line.
Python
1age = int(input("Age? "))
2if age >= 12
3    print("Teen")
3
Python does not start this program either. What is wrong, and how do you fix it?
Python
1age = int(input("Age? "))
2if age >= 12:
3print("Teen")
4
Follow the program for the speeds 50, 51 and 20. What is printed each time?
Python
1speed = int(input("Speed? "))
2if speed > 50:
3    print("Slow down")
4else:
5    print("Safe speed")