Puzzle 2 – create an AI bot to guess a number

Step 3 – finishing off

In the last part of the program all we need to do is to tell the user they got it right.

Scratch code image

This happes after the Repeat – Until loop has finished in Scratch, so it will only ever happen once and only when the player gets the answer right. In Python it needs to happen after the While loop.

Joining things together to show the number of guesses is a little tricky here.

IMPORTANT: this line of code has to go at the same indent level a the very first line of code in your program. So all the way to the left. You don't want it inside the loop at all.

  1. Use a print line to do this – the code should be back at the same indent level as the code at the beginning of the program
  2. Save your program
  3. Run your program
  4. Run it several times to check it always works right and to correct any errors

Code helper image

That's the basic program done. Now we can try something a bit more advanced.