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

Types, flags and fixed values

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.

Python
1temp = int(input("Temperature? "))
2too_hot = temp > 30
3print(too_hot)
Output
True
Worked examples
temp typedtemp > 30too_hot holds
34TrueTrue
30FalseFalse
12FalseFalse
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.
tempwindycoldPrinted
5yesTrueStay inside
5noTrueWear a coat
20yesFalseGo 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 <= 2
4if near and rain == "no":
5    print("Walk")
6elif near:
7    print("Take an umbrella")
8else:
9    print("Take the bus")
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.
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.
4
A pupil writes ready = "True" and then if ready: and thinks it tests a flag. What is wrong with the first line?