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.
Run the program a few times before you start. Once, guessing 3 and then 5
and then 7, and once guessing 7 straight away.
number = 7
print("I'm thinking of a number, can you guess it?")
guess = int(input())
while guess != number:
print("Wrong! Guess again!")
guess = int(input())
print("You guessed it!")
Which line does the iteration start on?
Line 4, the line that reads while guess != number:.
The three lines above it get the program ready: line 1 stores the number to be
guessed, line 2 asks the question and line 3 collects the first guess. None of
them repeats anything. The while is where the repeating starts, and the
iteration runs from there to the end of the indented block below it.
What does != mean?
Not equal to.
guess != number compares the two values and produces True when they are
different, and False when they are the same. It is a comparison operator, so
it does not change either variable; all it produces is the True or False
that the while needs.
It is the natural operator for this kind of loop, because the loop should keep going for exactly as long as the guess is not the number. Write the condition as what you do not want, and the loop stops as soon as you get what you do want.
How many variables are there in the code?
Two: number and guess.
number is assigned once, on line 1, and never changes. guess is assigned
twice, on line 3 and on line 6, and it is the one that changes: each time round
the loop it is given whatever the user has just typed. That is what lets the
condition produce a different answer next time.
int and input are not variables. They are things Python already knows how to
do: input collects text from the user, and int turns that text into a whole
number so that it can be compared with number.
How can you tell which lines of code are inside the loop?
By their indentation. Lines 5 and 6, print("Wrong! Guess again!") and
guess = int(input()), are indented further than the while line, so they are
the body of the loop and they are the lines that get repeated.
Line 7, print("You guessed it!"), starts back at the left hand margin, so it
is not part of the loop. It runs once, after the loop has finished.
Indentation is not decoration in Python: it is the only thing that says where the body of the loop stops.
How many times will the loop repeat?
There is no fixed answer. It depends entirely on what the user types.
7 first time and the condition is false straight away, so the body
never runs at all: zero repetitions.3 and then 7 and the body runs once.3, 5 and then 7 and it runs twice.7 and it never stops.That is the whole point of a conditional loop, and the difference between this loop and a counter loop. A counter loop is written to repeat a set number of times, and you can work out how many just by reading it. A loop that waits on the user repeats as many times as it takes, and the code alone cannot tell you how many that will be.
What has to happen to make the program run the last line of code?
The user has to type 7.
The last line only runs once the loop has finished, and the loop only finishes
when its condition is false. The condition is guess != number, so it is false
exactly when guess and number hold the same value, and number holds 7.
That is why You guessed it! can safely be printed without checking anything:
by the time the program gets there, the guess must have been right.
It also explains why line 6 matters so much. It is the only line that can change
guess, so it is the only thing that can ever make the condition false. Take it
away and the last line is unreachable: the program would print
Wrong! Guess again! forever.
Make sure that you check for the following things:
while.while line has a condition.while line has a colon (:) at the end.while, and all of
it by the same amount. Never mix tabs and spaces in one file.int().