In an investigate task you take working code apart to see how each piece contributes to the result. Answer every question for yourself first. You can edit the code and run it to check. Only then open the answer.
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!")
The answers below show everything the program prints, including the two lines it prints before it waits for your guess.
What happens if you input the guess 2?
I have thought of a number between 1 and 10
Can you guess what it is?
Too Low!
All three conditions are checked. 2 == 5 is false, so Correct! is skipped.
2 < 5 is true, so Too Low! is printed. 2 > 5 is false, so Too High! is
skipped.
What happens if you input the guess 6?
I have thought of a number between 1 and 10
Can you guess what it is?
Too High!
This time it is the third condition that is true. 6 == 5 is false and 6 < 5
is false, so those two branches are skipped, and 6 > 5 is true.
What happens if you input the guess 5?
I have thought of a number between 1 and 10
Can you guess what it is?
Correct!
5 == 5 is true, so the first branch runs. The other two conditions are both
false, because 5 is neither less than nor greater than itself. A number is
either smaller, or bigger, or exactly equal: never two of those at once, which
is why this program can only ever print one of the three messages.
What happens if you input the guess -5?
I have thought of a number between 1 and 10
Can you guess what it is?
Too Low!
The same branch as a guess of 2. Negative numbers are smaller than 5 just like
2 is, and the condition guess < number does not care how much smaller.
Notice what the program does not say. It was asked for a number between 1
and 10, and -5 is not one, but nothing in the code checks that. A condition
only ever answers the question you actually wrote.
What happens if you input the guess 0?
I have thought of a number between 1 and 10
Can you guess what it is?
Too Low!
Again the same branch. 0 is outside the range the program asked for, exactly
like -5, and again nothing checks for that. 0 is not a special value to
Python here; it is simply a number that is less than 5.
What do the symbols < and > mean in the conditions guess < number and
guess > number?
< means less than and > means greater than. Each one compares the
value on its left with the value on its right and answers True or False.
So guess < number asks “is the guess smaller than the number?” and
guess > number asks “is the guess bigger than the number?”. A useful way to
remember which is which: the symbol is narrow at the small end and wide at the
big end.
What happens if you change if guess == number: to if guess = number:?
The program refuses to run at all. It stops with an error before it prints anything, not even the first line:
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
A single = is the assignment symbol: it stores data in a variable. It is not a
question, so it cannot produce the True or False that an if needs, and
Python will not accept it in a condition.
The error message is worth reading, because Python 3 works out what you probably
meant and suggests == for you. A SyntaxError is always found before the
program starts, which is why nothing at all is printed first.
What do you notice about each line that follows each if statement?
Every one of them is indented, and every if line above them ends with a
colon.
The indentation is what makes those lines part of the branch, so they are the
lines that are skipped when the condition is false. If you removed the
indentation from print("Too Low!"), it would no longer belong to the if
above it, and the message would be printed for every guess, including a correct
one.
The program is built from three separate if statements rather than one. That
means Python checks all three conditions on every run, even once one of them has
already matched and printed a message.
Here that does no harm, because at most one of the three can be true at a time, so the program still prints exactly one message. It is wasted work, though, and it only stays correct as long as the three conditions cannot overlap. The next unit introduces a way of chaining conditions together so that the computer stops checking as soon as one of them matches.
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.