Drop links or images here to add them to the editor.

The last make task of the chapter extends the guessing game from the previous exercise. Start from your program for that one: everything it does, it still does here.

Your task

The game already tells the user whether their guess is too high or too low. Add one more thing:

Within 5 means a difference of 5 or less, either way. With the number 49, the guesses 44 to 48 and 50 to 54 are all close, 44 and 54 included. 43 and 55 are not. The right guess is never called close: it gets Well done! instead.

Everything else stays as it was in the previous exercise:

The output

Close! goes on its own line, straight after the too low or too high message, and before the next question.

Example

These examples show what happens when the number is 49 in the first run and 42 in the second. Your program will think of different numbers when you run it yourself.

Input:

43
44
55
54
49

Output:

I am thinking of a number between 1 and 100.
What is your guess?
Your guess is too low
What is your guess?
Your guess is too low
Close!
What is your guess?
Your guess is too high
What is your guess?
Your guess is too high
Close!
What is your guess?
Well done!
Number of guesses: 5

Input:

41
43
42

Output:

I am thinking of a number between 1 and 100.
What is your guess?
Your guess is too low
Close!
What is your guess?
Your guess is too high
Close!
What is your guess?
Well done!
Number of guesses: 3

The checks fix the random seed before your program runs, so they know the number in advance and can type in guesses that are exactly 5 and exactly 6 away from it. That only works if your program calls random.randint(1, 100) once, before the first guess.

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:

  • A guess is close when it is at least the number minus 5 and at most the number plus 5. That is a range check, written with >=, <= and and.
  • The check uses >= and <=, not > and <, so that a guess exactly 5 away counts as close.
  • The right guess is not close, so check that the guess is not the number before printing Close!.
  • Close! is printed after the too low or too high message, not instead of it.