Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Another modify task, and it carries on where the previous one stopped. A guessing game that gives you one guess is over very quickly. This one keeps going until you win.

The editor starts with the original guessing game again, with the secret number fixed at 7. Either paste in your program from the previous exercise, or make those changes again first: the random secret number, and the easy and hard modes.

Your task

Extend the game so that:

  1. It keeps asking for guesses until the user guesses correctly.
  2. It keeps track of the number of guesses the user takes, and tells them at the end.

Everything the previous exercise asked for still applies: the same two questions, random.randint called once after the mode is chosen, and a range of 1 to 10 in easy mode and 1 to 100 in hard mode.

Some details, so that your game can be checked automatically:

The output

The two questions and the guess question, as before:

Hello, what's your name?
Which mode do you want to play, easy or hard?
Okay NAME, guess a number between 1 and HIGHEST:

Then one message per guess, ending with the congratulations, and finally, where NUMBER is how many guesses the user took:

Number of guesses: NUMBER

Example

These examples show what happens when the secret number is 6 in the first run and 31 in the second. Your program will pick different numbers when you run it yourself.

Input:

Priya
easy
2
9
6

Output:

Hello, what's your name?
Which mode do you want to play, easy or hard?
Okay Priya, guess a number between 1 and 10:
Your guess is too low
Your guess is too high
Congratulations! You guessed correctly.
Number of guesses: 3

Input:

Ben
hard
40
40
20
31

Output:

Hello, what's your name?
Which mode do you want to play, easy or hard?
Okay Ben, guess a number between 1 and 100:
Your guess is too high
Your guess is too high
Your guess is too low
Congratulations! You guessed correctly.
Number of guesses: 4

Ben guessed 40 twice, and both count.

The checks fix the random seed before your program runs, exactly as in the previous exercise, so they know the secret number in advance and can type in guesses that eventually hit it. That only works if your program calls random.randint once, after the mode is known.

Ask each question with its own print, and read the answer with a bare input() on the next line, rather than passing the question to input(...). The questions are part of the output that is checked here, so their spelling, capital letters and punctuation count.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The loop is a while loop, and its condition keeps it going for as long as the guess is not the secret number (!=).
  • The guess has a value before the loop starts, one that can never be the secret number, so that the condition can be checked the first time.
  • The input() for the guess is inside the loop, so that each repetition reads a new guess.
  • The counter is created before the loop with the value 0, and increased by 1 inside the loop, once for every guess.
  • The print of the number of guesses is not indented, so it runs once, after the loop.