CMPS 5P Introduction to Programming in Python Programming Assignment 3 solved

$35.00

Category: You will receive a download link of the .ZIP file upon Payment

Description

5/5 - (1 vote)

In this assignment you will write a Python program that plays an interactive guessing game with the user.
Your program will generate a random integer in the range 1 to 10, then allow the user three guesses to
determine the integer. After each guess your program will inform the user whether the guess was correct,
too high, or too low. Your source file for this project will be called Guess.py. Here is a transcript of
several plays of the game. The symbol % stands for the command line prompt and is not typed by the user.
% python Guess.py
I’m thinking of an integer in the range 1 to 10. You have three guesses.
Enter your first guess: 5
Your guess is too high.
Enter your second guess: 3
Your guess is too high.
Enter your third guess: 1
You win!
% python Guess.py
I’m thinking of an integer in the range 1 to 10. You have three guesses.
Enter your first guess: 3
Your guess is too low.
Enter your second guess: 8
You win!
% python Guess.py
I’m thinking of an integer in the range 1 to 10. You have three guesses.
Enter your first guess: 4
Your guess is too low.
Enter your second guess: 9
Your guess is too high.
Enter your third guess: 5
Your guess is too low.
You lose. The number was 6.
Observe that there are blank lines at the beginning and end of program output, and blank lines separating
guesses. Your program should match the above format exactly.
Use the randint() method in the random module to generate the random number. You can read about this
module and it’s availabe methods in the Python module index.
http://docs.python.org/3/library/random.html#module-random
2
Once the mystery number is chosen, program operation will depend on user input. Your program must give
truthful responses to the user’s guesses then halt when either the right number is guessed, or the user runs
out of guesses. You will use the conditional operations if-elif-else to accomplish this. These
commands will be discussed at length in class, and can also be found in chapter 5 of the online text.
What to turn in
Submit your program Guess.py, to the assignment name pa3 before the due date. As always start early
and ask questions in lab sessions, office hours, and on piazza.
Discussion
Although this game is very simple, it is interesting to consider what strategy the user might adopt. Try to
verify for yourself that if we were to allow four guesses instead of three, then a strategy exists that would
find the mystery number with certainty, i.e. the guesser could always win. Similarly if we were to restrict
the number to the set {1, 2, 3, 4, 5, 6, 7}, then it can always be found in three guesses. The game, as defined
above, does posses an optimal strategy (optimal in the sense that it maximizes the probability winning.)
Try to deduce such a strategy, and show that by using it, the guesser will win 70% of the time. As an
exercise, write a program that plays the other side of the game (i.e. the role of guesser) and that implements
this optimal strategy. This exercise will yield no credit for the current assignment, but it may form the basis
of some future (more difficult) project.