Week 9 · 1.5 Data in text-based programs · 3 lessons
2
True, False and flags
Computing 8: Topic 1.5, pp. 106–124
GoalStore a comparison in a Boolean variable and use a Boolean variable, called a flag, in an if statement.
New words
Boolean variable · a variable that holds only True or Falseflag · a Boolean variable that signals whether something has happened or is the caseexpression · a combination of values and operators that Python works out
Explanation
The two Boolean values in Python are True and False. They start with a capital letter and have no quotation marks. In quotation marks, "True" is only a string. A comparison already gives a Boolean value, so it can be stored in a variable: too_hot = temp > 30. A Boolean variable used to signal something is called a flag. A flag can be used directly in a condition. We write if too_hot: and not if too_hot == True:. We write if not too_hot: for the opposite. Flags make programs easier to read, because a name such as too_hot says what the test means, and the test is written once. You can also join flags with and and or.
A comparison gives True or False, and the result is stored in a variable. With 34 typed, temp > 30 is True. The flag can then be shown or used later.
temp
windy
cold
Printed
5
yes
True
Stay inside
5
no
True
Wear a coat
20
yes
False
Go out
The program sets cold = temp < 10, prints Stay inside when cold is true and the wind is yes, prints Wear a coat when only cold is true, and otherwise prints Go out. With 5 and yes the program prints Stay inside. With 5 and no it prints Wear a coat. With 20 and yes it prints Go out.
Python
1print(True)
2print("True" == True)
3print(true)
Output
True
False
NameError: name 'true' is not defined
Spell Boolean values exactly. Only the first is a Boolean. The second compares text with a Boolean and gives False. The third is a NameError, because Python does not know a name written true.
Class activity
Flag cards (no computer). Write three flags on cards: raining, late and has_key. Beside each, write the condition it stands for, such as rain_mm > 0, minutes > 10 and key == "yes". Your partner reads out three facts about a morning, for example 4 mm of rain, 12 minutes late and no key. Hold up True or False for each flag. Then write the if lines that use the flags, such as if raining and late:. Invent a new flag and condition and test it with your partner. If you have a computer or tablet, ask an adult to run a small program that prints a flag.
Practice
1
Follow the program for the inputs (1, no), (2, yes) and (5, no). What is printed each time?
Python
1km = int(input("Distance in km? "))
2rain = input("Raining? ")
3near = km <= 24if near and rain == "no":
5print("Walk")
6elif near:
7print("Take an umbrella")
8else:
9print("Take the bus")
(1, no): near is True and rain is no, so Walk. (2, yes): near is True (2 <= 2), but rain is not no, so the elif runs and Take an umbrella is printed. (5, no): near is False, so Take the bus. The flag near is used in the same way as a condition.
2
Write one line that stores in the variable wet whether the rain amount is more than 5. Write the if line that uses it.
wet = rain > 5 and then if wet: ... The flag holds True or False, so if wet: is enough.
3
Write a Python program that asks for a score. A flag called high should store whether the score is at least 80. If high is True print Well done, otherwise print Keep trying.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): score = int(input("Score? ")) / high = score >= 80 / if high: / [indent] print("Well done") / else: / [indent] print("Keep trying")
4
A pupil writes ready = "True" and then if ready: and thinks it tests a flag. What is wrong with the first line?
"True" in quotation marks is a string, not a Boolean. The Boolean is True with no quotation marks. The program may seem to work now, but a text always counts as true, which will cause mistakes later.