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.
The game already tells the user whether their guess is too high or too low. Add one more thing:
Close!.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:
random.randint(1, 100), called once, at the start.I am thinking of a number between 1 and 100. is printed once, before the
first guess.What is your guess?.Your guess is too low or Your guess is too high.Well done! and then Number of guesses: NUMBER.Close! goes on its own line, straight after the too low or too high message,
and before the next question.
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.
Make sure that you check for the following things:
>=, <= and and.>= and <=, not > and <, so that a guess exactly 5 away
counts as close.Close!.Close! is printed after the too low or too high message, not instead of it.