A while loop keeps going until its condition turns false. That is exactly what
you want when the program has to wait for the user: keep asking until the
password is right, keep guessing until the number comes up. Nobody can say in
advance how many times such a loop will repeat.
Very often you know the answer in advance. Print a menu of five options. Ask for three numbers. Count down from ten. The number of repetitions is part of the plan, and the program does not have to work it out.
Python has a second kind of loop for those: the for loop.
for counter in range(10):
print(counter)
print("Finished")
Run it and you get the numbers 0 to 9, one per line, and then Finished.
Both kinds of loop have a name in the exam specification. A loop that repeats a
set number of times is count controlled, and a loop that repeats until
something changes is condition controlled. In Python those are for and
while.
Every for loop starts with the same four pieces.
for counter in range(10):
for says that this is a count controlled loop.counter is the loop counter: a variable that holds a different value
each time round.range(10) is the list of values the counter will take. Here that is 0, 1,
2, 3, 4, 5, 6, 7, 8, 9, so the loop repeats ten times.The counter is an ordinary variable, so you can call it what you like. counter
and count are clear when the loop is just repeating something, and i is what
you will see in a lot of code once loops get shorter. What you cannot do is pick
a name that is already in use for something else, because the loop overwrites it
on every repetition.
In a while loop you have to build the counter yourself, in the three places
you met in the previous chapter: create it before the loop, compare it in the
condition, and change it inside the body. Forget the third one and the loop
never ends.
counter = 1
while counter < 5:
print(counter)
counter = counter + 1
A for loop does all three for you.
for counter in range(1, 5):
print(counter)
Both of these print 1, 2, 3, 4. The second one cannot be given an infinite loop by accident, because there is no condition to get wrong and no step to leave out. That is the practical reason to use a for loop whenever you already know how many repetitions you need.
This is the same rule as before, and it is still the one that catches people out.
for count in range(3):
print("Hello")
print("There")
print("You")
Hello and There are indented, so both of them are inside the loop and both
of them run three times. You starts at the left hand margin, so it is not part
of the loop and it runs once, after the loop has finished. Move that last line
in by four spaces and the output changes completely.
The counter is not only there to count. You can use its value inside the body, which is how one short loop prints a whole times table.
number = 2
for count in range(1, 11):
print(count * number)
The counter takes the values 1 to 10, so the body prints 2, 4, 6 and on up to
number to 7 and you get the seven times table from the same four
lines.Make sure that you check for the following things:
for.range(...) to work
through.for line has a colon (:) at the end.for, and all of it
by the same amount. Never mix tabs and spaces in one file.str() before they are joined onto text with a +.