Puzzle 1 – guessing a number

Step 2 – set up the loop

Here's the second part of the Scratch program. This sets up a loop that keeps going until the player guesses the answer right.

Scratch code image

A loop is an example of using repetition in code. It repeats the code until the answer (the guess) is the same as the number.

There's one major thing that needs to happen different in Python here.

  1. Python doesn't have a Repeat – Until loop. So we need to use a While loop instead. This works a bit differently: we have to say "repeat this while the answer (the guess) does not equal the number. To say "does not equal" in Python we use != which is a bit odd.
  2. Add the while line, but use the variable names from your program, not thing1 and thing2

    IMPORTANT: you need to indent the next line of code

  3. Then add a line to get a guess from the player. Take care here – the int bit is needed to convert the input into a number (an integer). Watch your brackets
  4. Save your program
  5. Run your program – then use the Esc key at the top left of your keyboard to "kill" the program if you need to

Code helper image

That's the loop set up. Now we can deal with the things inside the "higher" and "lower" bits of the program.