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

Term 1 check-in

Week 11 · Review: Term 1 · 3 lessons
2

Python choices and types

Computing 8: Topics 1.1–1.6, pp. 9–136
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".

Worked examples
Python
1temp = float(input("Pool temperature? "))
2if temp < 24:
3    print("Too cold")
4elif temp <= 30:
5    print("Just right")
6else:
7    print("Too warm")
Output
Just right
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 showsType of the result
10 / 42.5real
"3" + "4"34string
int("8") + 19integer
bool("0")TrueBoolean
int(9.99)9integer
float(5)5.0real
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".
Python
1MAX_RIDERS = 6
2riders = int(input("Riders? "))
3full = riders >= MAX_RIDERS
4if full:
5    print("Bus full")
6else:
7    print("Seats left:", MAX_RIDERS - riders)
Output
Seats left: 2
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.
Python
1age = int(input("Age? "))
2member = input("Member? ")
3if age < 12 or member == "yes":
4    print("Cheap")
5elif not (age >= 65):
6    print("Standard")
7else:
8    print("Senior")
2
This program has three mistakes. Find them, name the kind of error, and write the correct program.
Python
1colour = input("Colour? ")
2size = int(input("Size? "))
3if colour = "red" AND size > 3
4    print("Packed")
3
The user types 6 and then 3. What are the two lines of output? Explain why they are different.
Python
1a = input("First? ")
2b = input("Second? ")
3print(a + b)
4print(int(a) + int(b))
4
Rewrite this pseudocode as a Python program, and say what it prints for 5 parcels and for 7 parcels.
Pseudocode
1CONSTANT FEE = 3
2INPUT parcels
3total ← parcels * FEE
4IF total > 20 THEN
5    OUTPUT "Bulk rate"
6ELSE
7    OUTPUT "Total: ", total
8ENDIF