Description
Assignment
This assignment should be completed in a single Python source (.py) file.
(1) Write a function called guessing_game() that lets the user play a guessing game. The
function should take the following steps:
‚ Generate a uniformly-distributed random number in the interval r1, 100s
‚ Prompt the user to enter a number (you may assume the user does, in fact, enter an
integer, though not that it’s a number in the appropriate range)
‚ Display a message to the user letting them know whether their guess was too high, too
low, or correct
‚ If the guess was incorrect, prompt the user for another guess (again, assume the input
is a valid integer)
‚ If the guess was correct, congratulate the user and tell them how many guesses it took
to reach the correct answer
(2) Write a function called reversed_guessing_game() in which the computer guesses a
secret number that the user has thought of in the interval r1, 100s. The computer should
always succeed in 7 guesses or fewer. After each of the computer’s guesses, it should prompt
the user to enter one of L (if the guess was lower than the secret number), H (if the guess
was higher than the secret number) or C (if the guess was correct). You may assume the
user’s response is always one of those three possibilities.
(3) Make a copy of your solution to part 2 and modify it in the following ways.
‚ Add two parameters to the function so that its signature is
reversed_guessing_game(min, max). The arguments min and max identify
the range of values that the computer expects the user to select from (specifically, the
1
interval rmin, maxs). Modify the body so that the computer will correctly guess a
secret number in that range.
‚ Make the input handling more flexible. Permit the user to type lowercase characters as
well as uppercase characters in in their response. You may still assume the user’s input
is one of L, l, H, h, C, or c.
‚ Add a “cheat detector” to identify when the user tries to pull a fast one on the computer
and gives inconsistent responses. Detecting the nature of the cheating is quite challenging (for example, whether they picked a number outside of the range, they changed their
number mid-game, or they gave a wrong response to one of the computer’s guesses);
simply notifying the user that they have cheated is sufficient.
(4) Nothing to submit for this; just give it a bit of thought. Generalize the result from part 2
that “the computer should always succeed in 7 guesses or fewer” for your solution to part 3.
How many guesses will it take the computer to guess correctly, assuming the user is truthful
and consistent in their responses?
Submission
To submit this assignment, upload your .py file containing all three functions to the “Homework
1” assignment on Carmen. As always, be sure to note all group members who contributed to the
assignment and what those contributions were.
2