Challenge 34

🎲

Guess the Number!

Open in Code Editor or Copy Instructions
...solutions at bottom of page...

Create a "guess the number" game - the player should be prompted to choose from one of two modes:

Whatever option is chosen, the number to guess should be within a fixed range - for example, 1 to 100

The player will take guesses and will be notified if their guess is too high, too low or correct. An example game is shown below. Assume the number to guess is 72

Examples:

Player guesses 40 outputs "Too low, try again"

Player guesses 65 outputs "Too low, try again"

Player guesses 80 outputs "Too high, try again"

Player guesses 75 outputs "Too high, try again"

Player guesses 72 outputs "That's correct! The number was 72. It took you 5 guesses to win"

Extension:

For the guesser, there is an optimal strategy - do you know what it is?

Likewise, if a person is using this strategy, there will be numbers that will be the most difficult to guess - do you know what they would be?

Solutions

IGCSE 0478 / O-Level 2210 A-Level 9618