The while loop: interactive#

We have already demonstrated the syntax for for loops. But sometimes looping over a set of numbers is not appropriate. For example if the problem is along the lines of “Store the first X numbers satisfying Y” you might not know how many numbers to consider to meet that criterion. With a while loop you can avoid this.

Here’s an example: We have a hot drink and add ice cubes to it until it reaches drinking temperature.

def cooling_hot_water(drinking_temp):
    boiling_temp = 100 # boiling water temp. 100 degrees
    current_temp = boiling_temp # current temp of the drink
    ice_cube_temp  = -5 # adding one ice cube drops the temp by 5 degrees
    number_of_ice_cubes = 0 # number of ice-cubes that has been added so far
    while current_temp > drinking_temp:
        current_temp += ice_cube_temp
        number_of_ice_cubes += 1
        print(f"The current temperature of your drink is {current_temp}, number of ice cubes added to the drink is {number_of_ice_cubes}")
    return current_temp
#This block calls the function above and returns an answer
drinking_temp = 35
cooling_hot_water(drinking_temp)
The current temperature of your drink is 95, number of ice cubes added to the drink is 1
The current temperature of your drink is 90, number of ice cubes added to the drink is 2
The current temperature of your drink is 85, number of ice cubes added to the drink is 3
The current temperature of your drink is 80, number of ice cubes added to the drink is 4
The current temperature of your drink is 75, number of ice cubes added to the drink is 5
The current temperature of your drink is 70, number of ice cubes added to the drink is 6
The current temperature of your drink is 65, number of ice cubes added to the drink is 7
The current temperature of your drink is 60, number of ice cubes added to the drink is 8
The current temperature of your drink is 55, number of ice cubes added to the drink is 9
The current temperature of your drink is 50, number of ice cubes added to the drink is 10
The current temperature of your drink is 45, number of ice cubes added to the drink is 11
The current temperature of your drink is 40, number of ice cubes added to the drink is 12
The current temperature of your drink is 35, number of ice cubes added to the drink is 13
35

As soon as the while condition is not met the loop exits. You could also write this like:

def cooling_hot_water(drinking_temp):
    boiling_temp = 100 # boiling water temp. 100 degrees
    current_temp = boiling_temp # current temp of the drink
    ice_cube_temp  = -5 # adding one ice cube drops the temp by 5 degrees
    number_of_ice_cubes = 0 # number of ice-cubes that has been added so far
    while True:
        if current_temp <= drinking_temp:
            break
        current_temp += ice_cube_temp
        number_of_ice_cubes += 1
        print(f"The current temperature of your drink is {current_temp}, number of ice cubes added to the drink is {number_of_ice_cubes}")
    return current_temp

drinking_temp = 35
cooling_hot_water(drinking_temp)
The current temperature of your drink is 95, number of ice cubes added to the drink is 1
The current temperature of your drink is 90, number of ice cubes added to the drink is 2
The current temperature of your drink is 85, number of ice cubes added to the drink is 3
The current temperature of your drink is 80, number of ice cubes added to the drink is 4
The current temperature of your drink is 75, number of ice cubes added to the drink is 5
The current temperature of your drink is 70, number of ice cubes added to the drink is 6
The current temperature of your drink is 65, number of ice cubes added to the drink is 7
The current temperature of your drink is 60, number of ice cubes added to the drink is 8
The current temperature of your drink is 55, number of ice cubes added to the drink is 9
The current temperature of your drink is 50, number of ice cubes added to the drink is 10
The current temperature of your drink is 45, number of ice cubes added to the drink is 11
The current temperature of your drink is 40, number of ice cubes added to the drink is 12
The current temperature of your drink is 35, number of ice cubes added to the drink is 13
35

Here while True means “Loop until you manually tell me to stop”. True will always be true. The break statement is used here to break from the loop. Be careful with while loops, it is possible to write cases that will never exit and will run forever … Like this example:

def cooling_hot_water(drinking_temp):
    boiling_temp = 100 # boiling water temp. 100 degrees
    current_temp = boiling_temp # current temp of the drink
    ice_cube_temp  = -5 # adding one ice cube drops the temp by 5 degrees
    number_of_ice_cubes = 0 # number of ice-cubes that has been added so far
    while True:
        if current_temp <= drinking_temp:
            break
        # current_temp += ice_cube_temp  <- WHOOPS, forgot to cool the drink down when ice cube is added!!
        number_of_ice_cubes += 1
        print(f"The current temperature of your drink is {current_temp}, number of ice cubes added to the drink is {number_of_ice_cubes}")
    return current_temp

drinking_temp = 35
cooling_hot_water(drinking_temp)

You now need to use the stop button to tell the computer to stop running this! Go to Runtime -> Interrupt execution

The continue statement#

Let me also introduce the continue statement. This means “move to the next iteration of the loop”. For example in this case we want to print the first 100 numbers which are multiples of 5 or of 7, but we don’t want to print a number at all if it’s a multiple of both … In this case ands and ors could also do this, but it illustrates the point.

def print_multiples_of_N_or_M(N, M, num_to_print):
    """Print the first num_to_print multiples of N or M, but not both, starting from 1."""
    # In our example N is 7, M is 5, and num_to_print is 100, but these could be other values
    number_printed = 0
    current_number = 1
    while True:
        if (current_number % N == 0) and (current_number % M == 0):
            # Multiple of both, don't consider any carry on
            current_number += 1
            continue # GO TO THE NEXT ITERATION OF THE FOR/WHILE LOOP.
        if (current_number % N == 0):
            number_printed += 1
            print("{} is a multiple of {} and I've printed {} numbers".format(current_number, N, number_printed))
        if (current_number % M == 0):
            number_printed += 1
            print("{} is a multiple of {} and I've printed {} numbers".format(current_number, M, number_printed))

        current_number += 1

        if number_printed == num_to_print:
            break # break is used to exit a while, or for, loop NOW.

print_multiples_of_N_or_M(5, 7, 100)
5 is a multiple of 5 and I've printed 1 numbers
7 is a multiple of 7 and I've printed 2 numbers
10 is a multiple of 5 and I've printed 3 numbers
14 is a multiple of 7 and I've printed 4 numbers
15 is a multiple of 5 and I've printed 5 numbers
20 is a multiple of 5 and I've printed 6 numbers
21 is a multiple of 7 and I've printed 7 numbers
25 is a multiple of 5 and I've printed 8 numbers
28 is a multiple of 7 and I've printed 9 numbers
30 is a multiple of 5 and I've printed 10 numbers
40 is a multiple of 5 and I've printed 11 numbers
42 is a multiple of 7 and I've printed 12 numbers
45 is a multiple of 5 and I've printed 13 numbers
49 is a multiple of 7 and I've printed 14 numbers
50 is a multiple of 5 and I've printed 15 numbers
55 is a multiple of 5 and I've printed 16 numbers
56 is a multiple of 7 and I've printed 17 numbers
60 is a multiple of 5 and I've printed 18 numbers
63 is a multiple of 7 and I've printed 19 numbers
65 is a multiple of 5 and I've printed 20 numbers
75 is a multiple of 5 and I've printed 21 numbers
77 is a multiple of 7 and I've printed 22 numbers
80 is a multiple of 5 and I've printed 23 numbers
84 is a multiple of 7 and I've printed 24 numbers
85 is a multiple of 5 and I've printed 25 numbers
90 is a multiple of 5 and I've printed 26 numbers
91 is a multiple of 7 and I've printed 27 numbers
95 is a multiple of 5 and I've printed 28 numbers
98 is a multiple of 7 and I've printed 29 numbers
100 is a multiple of 5 and I've printed 30 numbers
110 is a multiple of 5 and I've printed 31 numbers
112 is a multiple of 7 and I've printed 32 numbers
115 is a multiple of 5 and I've printed 33 numbers
119 is a multiple of 7 and I've printed 34 numbers
120 is a multiple of 5 and I've printed 35 numbers
125 is a multiple of 5 and I've printed 36 numbers
126 is a multiple of 7 and I've printed 37 numbers
130 is a multiple of 5 and I've printed 38 numbers
133 is a multiple of 7 and I've printed 39 numbers
135 is a multiple of 5 and I've printed 40 numbers
145 is a multiple of 5 and I've printed 41 numbers
147 is a multiple of 7 and I've printed 42 numbers
150 is a multiple of 5 and I've printed 43 numbers
154 is a multiple of 7 and I've printed 44 numbers
155 is a multiple of 5 and I've printed 45 numbers
160 is a multiple of 5 and I've printed 46 numbers
161 is a multiple of 7 and I've printed 47 numbers
165 is a multiple of 5 and I've printed 48 numbers
168 is a multiple of 7 and I've printed 49 numbers
170 is a multiple of 5 and I've printed 50 numbers
180 is a multiple of 5 and I've printed 51 numbers
182 is a multiple of 7 and I've printed 52 numbers
185 is a multiple of 5 and I've printed 53 numbers
189 is a multiple of 7 and I've printed 54 numbers
190 is a multiple of 5 and I've printed 55 numbers
195 is a multiple of 5 and I've printed 56 numbers
196 is a multiple of 7 and I've printed 57 numbers
200 is a multiple of 5 and I've printed 58 numbers
203 is a multiple of 7 and I've printed 59 numbers
205 is a multiple of 5 and I've printed 60 numbers
215 is a multiple of 5 and I've printed 61 numbers
217 is a multiple of 7 and I've printed 62 numbers
220 is a multiple of 5 and I've printed 63 numbers
224 is a multiple of 7 and I've printed 64 numbers
225 is a multiple of 5 and I've printed 65 numbers
230 is a multiple of 5 and I've printed 66 numbers
231 is a multiple of 7 and I've printed 67 numbers
235 is a multiple of 5 and I've printed 68 numbers
238 is a multiple of 7 and I've printed 69 numbers
240 is a multiple of 5 and I've printed 70 numbers
250 is a multiple of 5 and I've printed 71 numbers
252 is a multiple of 7 and I've printed 72 numbers
255 is a multiple of 5 and I've printed 73 numbers
259 is a multiple of 7 and I've printed 74 numbers
260 is a multiple of 5 and I've printed 75 numbers
265 is a multiple of 5 and I've printed 76 numbers
266 is a multiple of 7 and I've printed 77 numbers
270 is a multiple of 5 and I've printed 78 numbers
273 is a multiple of 7 and I've printed 79 numbers
275 is a multiple of 5 and I've printed 80 numbers
285 is a multiple of 5 and I've printed 81 numbers
287 is a multiple of 7 and I've printed 82 numbers
290 is a multiple of 5 and I've printed 83 numbers
294 is a multiple of 7 and I've printed 84 numbers
295 is a multiple of 5 and I've printed 85 numbers
300 is a multiple of 5 and I've printed 86 numbers
301 is a multiple of 7 and I've printed 87 numbers
305 is a multiple of 5 and I've printed 88 numbers
308 is a multiple of 7 and I've printed 89 numbers
310 is a multiple of 5 and I've printed 90 numbers
320 is a multiple of 5 and I've printed 91 numbers
322 is a multiple of 7 and I've printed 92 numbers
325 is a multiple of 5 and I've printed 93 numbers
329 is a multiple of 7 and I've printed 94 numbers
330 is a multiple of 5 and I've printed 95 numbers
335 is a multiple of 5 and I've printed 96 numbers
336 is a multiple of 7 and I've printed 97 numbers
340 is a multiple of 5 and I've printed 98 numbers
343 is a multiple of 7 and I've printed 99 numbers
345 is a multiple of 5 and I've printed 100 numbers

Try it yourself#

Write a function to print the first 20 integers greater than 0 that:

  • Are even

  • Are multiples of 3.