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

Words that join conditions

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

Trace it, then fix it

Computing 8: Topic 1.4, pp. 70–105
GoalTrace programs with several inputs, and recognise the common mistakes that stop a program or make it give wrong answers.
New words
trace · follow a program by hand, step by step, with chosen inputsruntime error · a mistake that stops a program while it is runningtest data · values chosen to try out a program
Explanation

To trace a program that uses and and or, make a table with one row for each set of inputs. Write the result of each small condition and then the output. Choose inputs that make every branch run at least once. Mistakes fall into three families. A syntax error breaks the rules, so the program does not start, for example a missing colon or a capital AND. A runtime error stops the program while it runs, for example comparing text with a number using > or using a variable that has no value. A logic error lets the program run, but the answer is wrong, for example conditions in the wrong order or or where and was needed. Read the last line of the message, because it tells you the type and where to look.

MistakeExample lineFamily
capital ANDif a > 1 AND b < 9:syntax error
text compared with a numberif age > 12: (age is still text)runtime error
else ifelse if x > 3:syntax error
wrong order of conditionsif n >= 10 before n >= 20logic error
Worked examples
slipmoneyslip == "yes"money >= 10Printed
yes10TrueTrueCan go
yes5TrueFalseAlmost ready
no10FalseTrueAlmost ready
no5FalseFalseNot ready
The school trip program prints Can go when both conditions are true, Almost ready when only one is true and Not ready when neither is true. Here it is traced with four sets of inputs.
LineWhat goes wrongFamily
if x > 5 and < 9:right side is not a complete conditionsyntax error
if x = 5:= stores a value, == comparessyntax error
if x > 5 or x < 9: (meaning between)true for every numberlogic error
print(total) before total has a valueNameErrorruntime error
A table of common errors with the type of each. Notice that a logic error does not show any message at all.
Python
1n = int(input("Number? "))
2if n >= 1 or n <= 9:
3    print("Accepted")
4else:
5    print("Rejected")
Output
Accepted
A short program with an or in a range test: the intention was to print Accepted for numbers from 1 to 9. It accepts every number.
Class activity

Bug hunt (no computer). Write six short Python lines on separate slips of paper. Make three of them correct and put one mistake in each of the others: a capital AND, a missing colon, and an or where an and is needed. Give the slips to a partner, who sorts them into three piles: correct, program will not start, and program runs but is wrong. Check the piles together and explain each mistake in one sentence. Swap roles. If you have a computer or tablet, try the slips with an adult and compare the real messages with your piles.

Practice
1
Trace the program for the four pairs of inputs. What is printed each time?
Python
1pass_type = input("Pass? ")
2age = int(input("Age? "))
3if pass_type == "day" and age < 12:
4    print("Child day pass")
5elif pass_type == "day" or age < 12:
6    print("Check the price")
7else:
8    print("Full price")
2
Name the family of mistake (syntax, runtime or logic) for each line: (a) if x > 5 AND x < 9: (b) if x > 5 or x < 9: when you want numbers between 5 and 9 (c) if x == 5 with no colon (d) if age > 12: when age came from input without int().
3
Write a Python program: ask for the day. If it is Saturday or Sunday print Weekend, otherwise print School day.
4
Rewrite in Python: IF NOT (lives = 0) AND level >= 3 THEN OUTPUT "Bonus" ENDIF