In a predict and run task you do not write code from scratch. You start from a small piece of working code, work out what you think it will do before you run it, and then run it to check whether you were right.
This task contains three examples. Work through them in order, because each one adds something to the one before it.
For every example, study the code carefully and write down a prediction of what
it will do when it runs. Your prediction should use the key terms
selection, condition and branch. You can type your prediction
straight into the code as a comment, on a line starting with #.
Say which line does the selecting, what the condition is, whether that condition is true or false, and which branch runs as a result.
Only once you have written your prediction should you run the code and compare the result with what you expected.
Need more help with if statements?
Click here.
Selection with one outcome. Read the value stored in age and the condition on
the if line very slowly, one character at a time, before you decide what this
outputs.
age = 18
if age > 18:
print("You are old enough to drive")
Predict how many lines of output you expect, then run it. If what you get is not what you predicted, do not change the code: work out why first, then open the discussion below.
The program outputs nothing at all.
age holds exactly 18, and the condition asks whether age is greater than
False, the branch under the
if is skipped, and the program reaches the end without ever printing anything.Nothing is broken. This is selection with one outcome doing exactly what it is
supposed to do: there is no else, so when the condition is false there is
simply nothing left to run.
Two single-character changes each make the message appear, and they are worth trying separately:
>=, so the condition asks “greater than or equal
to 18”. An 18 year old can drive, so this is almost certainly what the
program was meant to say.age = 19, leaving the condition alone. Now the data
satisfies the condition instead of the condition being widened to fit the
data.The boundary value, the one exactly on the line, is where > and >= disagree,
and it is the value worth checking every time you write a condition.
Selection with two outcomes. This one always outputs exactly one line, so the question is which of the two.
num1 = 1337
if num1 == 10:
print("This text is output because the condition was true")
else:
print("This text is output because the condition was false")
Predict which of the two lines you will see, then run it. Then change num1 to
10 and run it again, and check that the other branch takes over.
Three conditions in a row, on a number the user types in. The int() around the
input() converts the answer from text into a whole number so that it can be
compared with number.
number = 5
print("I have thought of a number between 1 and 10")
print("Can you guess what it is?")
guess = int(input())
if guess == number:
print("Correct!")
if guess < number:
print("Too Low!")
if guess > number:
print("Too High!")
Predict what happens for a guess of 2, then run it and type 2. Run it again
with 8, and a third time with 5.
There are three separate if statements here rather than one long one, so
Python checks all three conditions every single time, even after one of them has
already matched. Work out how many of the three can be true at once, and what
that means for the number of lines the program can output.
Make sure that you check for the following things:
:) at the end of every if line and every else line.else.==) in a condition that checks whether
two pieces of data are the same. A single = stores data in a variable.input, and the second closing bracket
that int(input()) needs.