design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.

use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs


LO: To know what a loop is.

Now that we know how to write a Python program to work out mathematical problems it is time to learn about Conditions, features of programming, which perform different computations or actions depending on whether a condition evaluates to true or false (or to some other value).

What does this mean?

We can look at this as how a computer program can react to other things. Let's look at some real world examples:

We will start by looking more closely at problems 3 and 4 by introducing the while loop:

Looking at this flow chart we can see that a while loop will check if a conditioin is True or False, if it is True then the commands inside the loop will be carried out and repeated until the condition is False - at that time the loop will break. Let's look at our two examples in code:

#while the temperature is less than 20, turn the radiators on.
while temperature < 20:
    radiators = True


We declare our loop by using the while command followed by the condition that it will evaluate - in this case if a variable called temperature is less than 20: it is True if it is, and False if it is not. This condition is followed by a colon : indictating to Python that the following indented commands are to be carried out in the loop. The only command here is assigning a variable called radiators the value True (we can imagine another function() that will only run if the variable radiators has a value of True that turns the radiators in the house on.
Looking back on the flow chart we can see that once the temperature is greater than 20 the loop will not run - it will no longer be True that temperature < 20 *.

#While we are playing the game, you have to roll the die.

play = True

while play:


In this second example we find another variable that has the value True, play = True. When we set a variable to either True or False (sometimes refered to as flag variables) they can be evaluated in while loops and other conditions that we will look at - they are very useful in starting and stopping things. Here there is a variable called play that is responsible for starting and stopping the game - if it is True then we play the game (roll the die etc.), if it is False then everything stops, it is the end of the game. You can imagine a function() at the beginning of the program setting the variable value to True: "do you want to play?" and a function() setting the value to False at the end of the game: reaching a certain score etc.

To explore these loops we will use Reeborg. Open the world Around 1 (see resources below). Here we will write our first while loop, notice how True and False are always written in capitals:

while True:


Having run this program you will see that Reeborg will move forward until it hits into the wall, but why?

The conditional while will look to see if the condition True is True - it is literally True it has to be!!! Then it will execute the code associated with it - just like writing a function, ending in a colon : signals instructions to be carried out. While the condition is True it will continue to run the code associated with it - Reeborg will move forward forever because True will always be True.

Getting Reeborg to say "Hello" forever - because True is always True - can also be achieved:

while True:


You will need to press the 'stop' button to end this program because otherwse it will not end (Reeborg will stop after a certain number of instructions) because True is always True.

Experiment with what you can make Reeborg do using a while loop. (Remember to use the stop button!).

* This will not actually result in the radiators being turned off - can you work out why?

Code Examples