If the user responds with 0, then you will print out "Thank you for playing the Guessing Game." and the program should terminate. Your program should correctly guess the number in seven attempts or less. If after seven tries the user still replies with either 1 or -1, you will print out "Either you guessed a number out of range or you had an incorrect entry." and the program should terminate.
Recognize that this is a binary search problem. You will have to adapt the binary search algorithm to suit this problem. You can create two variables lo and hi and initialize them to 1 and 100 respectively. You will be offering mid which is the average of lo and hi as your guess. The user will be doing the comparison and letting you know whether you have found the element, or whether your guess was too high or too low. And you will refine your search according to the response of the user.
If the user did guess a number between 1 and 100 you should find it in 7 attempts or less. Now the user may have entered 1 for -1 or vice versa. In such a case you will never be able to guess the right answer. If on the 7th guess the user enters a 1 or -1, then issue the following statement:
Either you guessed a number out of range or you had an incorrect entry.Here is a sample session, where the program guesses correctly:
Guessing Game Think of a number between 1 and 100 inclusive. And I will guess what it is in 7 tries or less. Are you ready? (y/n): y Guess 1 : The number you thought was 50 Enter 1 if my guess was high, -1 if low, and 0 if correct: 1 Guess 2 : The number you thought was 25 Enter 1 if my guess was high, -1 if low, and 0 if correct: -1 Guess 3 : The number you thought was 37 Enter 1 if my guess was high, -1 if low, and 0 if correct: 0 Thank you for playing the Guessing Game.
If the user enters a y for the question Are you ready? then go through with the series of guesses. If he enters n print Bye and exit the program. On the other hand if the response is anything other than y or n then keep repeating the question. If the response regarding a guess is anything other than 1, 0, or -1, then keep repeating the guess.
The file that you will be turning in will be called GuessingGame.py. You will follow the standard Python coding convention that I have discussed in class. We will also be looking for meaningful variable names and good documentation. The file will have a header of the following form:
# File: GuessingGame.py # Description: # Student Name: # Student UT EID: # Course Name: CS 303E # Unique Number: # Date Created: # Date Last Modified:
Use the turnin program to submit your GuessingGame.py file. The proctors should receive your work by 11 PM on Wednesday, 20 November 2013. There will be substantial penalties if you do not adhere to the guidelines. The TA in charge of this assignment is Lee Thompson (firstname.lastname@example.org).