GoalRead, predict and correct Python programs that use if, elif, else, and, or, not, casting and Boolean values.
New words
cast · to change a value from one data type to another, for example with int()Boolean · a value that is True or False, written with capital letters and no quotesflag · a variable that stores True or False so a program can remember a decisionsyntax error · a mistake in the rules of the language that stops the program from starting
Explanation
In Python a choice is made with if, elif and else. Each condition ends with a colon, and the lines inside it are indented. A chain of elif tests runs only the first branch that is true. The logic words and, or and not are written in lower case. The function input() always gives text, so a number must be cast with int() or float() before it is compared or used in a sum. Text is case-sensitive, so "Yes" is not the same as "yes". A Boolean value is True or False, and a flag is a variable that remembers one. Be careful with bool(): any non-empty string is True, even the string "False".
input() gives text→int() or float()→compare or calculate→if / elif / else
The pool thermometer program. The user types 24. The text is cast to the real number 24.0. Then 24 < 24 is false, so Python moves to elif. 24 <= 30 is true, so it prints Just right. The boundary values 24 and 30 both belong to Just right, because the first test uses < and the second uses <=.
Expression in print()
Python shows
Type of the result
10 / 4
2.5
real
"3" + "4"
34
string
int("8") + 1
9
integer
bool("0")
True
Boolean
int(9.99)
9
integer
float(5)
5.0
real
Six small expressions and what they give. Casting changes the type of a value. The result of / is always a real number. A string made of digits is still a string until it is cast, and any non-empty string gives True with bool(), even "0".
A bus has a limit of 6 riders, stored as a constant. The flag full stores the result of a comparison. For 4 riders the flag is False, so the else branch shows the seats left. For 6 or more riders the flag is True.
Class activity
Error detectives (no computer). On a strip of paper write a four-line Python program that asks one question and uses if and else. Then spoil it with exactly one mistake: a missing colon, = instead of ==, a capital AND, or a missing cast. Swap strips. Your partner must find the mistake, name the kind of error (syntax error, runtime error or logic error) and write the corrected line. Keep a score of how many each player finds. If you have a computer or tablet, an adult can help you run the spoiled program and compare Python’s message with your guess.
Practice
1
What does the program print for each input? (a) age 10, member no (b) age 30, member no (c) age 70, member no (d) age 70, member yes (e) age 65, member no.
(a) Cheap, because age < 12 is true. (b) Standard, because not (30 >= 65) is true. (c) Senior, because both earlier tests are false. (d) Cheap, because member == "yes" is true. (e) Senior, because 65 >= 65 is true, so not is false.
2
This program has three mistakes. Find them, name the kind of error, and write the correct program.
All three are syntax errors. (1) = should be == in a comparison. (2) AND should be and, in lower case. (3) The if line needs a colon at the end. Corrected: if colour == "red" and size > 3:. The program then prints Packed for red and 5, but nothing for red and 3 or for Red and 5, because the text is case-sensitive.
3
The user types 6 and then 3. What are the two lines of output? Explain why they are different.
63 and then 9. The values from input() are text, so a + b joins the two texts into 63. After int() the values are numbers, so the sum is 9.
4
Rewrite this pseudocode as a Python program, and say what it prints for 5 parcels and for 7 parcels.
Pseudocode
1CONSTANT FEE = 32INPUT parcels
3total ← parcels * FEE
4IF total > 20 THEN
5 OUTPUT "Bulk rate"6ELSE
7 OUTPUT "Total: ", total
8ENDIF
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): FEE = 3 / parcels = int(input("Parcels? ")) / total = parcels * FEE / if total > 20: / [indent] print("Bulk rate") / else: / [indent] print("Total:", total). Note: for 5 parcels it prints Total: 15. For 7 parcels the total is 21, which is more than 20, so it prints Bulk rate.