Escape Room Program – Part 2: making decisions

Step 2 – Repeating

The next thing we need to do is to get the program to repeat asking the question until the player enters "light". They can't move on until they can see.

We need a block of code which will let us repeat the question until the user types in "light".

This needs to use a while loop. A loop is just a block of code that repeats the same thing until something happens.

You might remember using while True a lot when you were programming micro:bits in Year 8. All that did was keep doing the same thing forever. We need to be a little bit cleverer now.

Add the loop

Code helper imageTo get the loop to work we need to use another variable. I'm going to call my variable move_on and I'll start by setting it to False – because I don't want to move on yet.

  1. Start by adding this line of code after the input line but BEFORE the IF – ELSE block: move_on = False

    IMPORTANT: False has a capital letter at the start

  2. Code helper imageNow, we need to add the while loop. This needs to be done exactly like in the screenshot.

    The : at the end of the line is REALLY IMPORTANT

  3. Include the IF block you already have in the loop – don't retype it.

    You have to indent your IF – ELSE block. Do this by adding four spaces tp the start of each line so that it looks EXACTLY like the next screenshot

    It's REALLY IMPORTANT to add the indents in the right way here. We usually use four spaces at the front of each indent. Python will do a lot of this for you, but you'll need to add some yourself. The lines all need to line up or else it won't work.


  4. The line that says move_on = True changes the value of the move_on variable. This tells the program that you want to move on to the next bit and will end the loop.
  5. Run your program and type in "light" when you're asked what you want to do

    This should work and the program should end properly.

  6. Now run the program again and try typing anything else ("Look", for example)

    Code helper imageThis causes a big problem! The program keeps running for ever. It won't stop!!

  7. To stop the program running wild, click File > Close at the top. Say Yes when the box asks you if you want to kill the program

We'll solve this problem in the next step...