Starting from:

$20

Programming Assignment 5 Solution

In this assignment you will write a Python program that plays the other side of the guessing game you implemented in pa3. Your program will ask the user for an initial search space by prompting for two integers giving the lower and upper limits respectively. If these two integers are out of order (i.e. low
high) it will continue to prompt for appropriate limits. (It is not necessary to recover from non-integer input at this stage.) Your program will then ask the user to think of a number in the given range. It will make a sequence of guesses that emulate the Binary Search algorithm discussed in class. (See the example BinarySearch.py posted on the class webpage.) It will ask the user if the mystery number is Less than, Greater than or Equal to each guess. Subsequent guesses will depend on how the user answers these queries.
If at some point the user acknowledges that the guess is equal to the mystery number, your program will state the number of guesses used. If at some point the current search space consists of just one number your program will stop guessing, announce the number and state the number of guesses used. If at some point the search space becomes empty, your program will state that the answers given by the user were inconsistent.
Your program will be called Question.py. A typical session with the user is reproduced below.
$ p y t h o n Q u e s t i o n . p y
E n t e r t w o n u m b e r s , l o w t h e n h i g h .
l o w = 1 0
h i g h = 1
P l e a s e e n t e r t h e s m a l l e r f o l l o w e d b y t h e l a r g e r n u m b e r .
l o w = 5
h i g h = 3
P l e a s e e n t e r t h e s m a l l e r f o l l o w e d b y t h e l a r g e r n u m b e r .
l o w = 1
h i g h = 1 0
T h i n k o f a n u m b e r i n t h e r a n g e 1 t o 1 0 .
I s y o u r n u m b e r L e s s t h a n , G r e a t e r t h a n , o r E q u a l t o 5 ?
T y p e ' L ' , ' G ' o r ' E ' : x
P l e a s e t y p e ' L ' , ' G ' o r ' E ' : y
P l e a s e t y p e ' L ' , ' G ' o r ' E ' : e
I f o u n d y o u r n u m b e r i n 1 g u e s s .
$
Observe how the user is prompted repeatedly if the limits are out of order. Notice also the blank lines that appear before and after user input. Your program will accept only the letters L, G, or E (upper or lower case) as valid input, and continues to prompt if any other input is supplied. Two more sessions follow.

$ p y t h o n Q u e s t i o n . p y
E n t e r t w o n u m b e r s , l o w t h e n h i g h .
l o w = 2
h i g h = 1 8
T h i n k o f a n u m b e r i n t h e r a n g e 2 t o 1 8 .
I s y o u r n u m b e r L e s s t h a n , G r e a t e r t h a n , o r E q u a l t o 1 0 ?
T y p e ' L ' , ' G ' o r ' E ' : g
I s y o u r n u m b e r L e s s t h a n , G r e a t e r t h a n , o r E q u a l t o 1 4 ?
T y p e ' L ' , ' G ' o r ' E ' : L
I s y o u r n u m b e r L e s s t h a n , G r e a t e r t h a n , o r E q u a l t o 1 2 ?
T y p e ' L ' , ' G ' o r ' E ' : L
Y o u r n u m b e r i s 1 1 . I f o u n d i t i n 3 g u e s s e s .
$ p y t h o n Q u e s t i o n . p y
E n t e r t w o n u m b e r s , l o w t h e n h i g h .
l o w = 1
h i g h = 1 0
T h i n k o f a n u m b e r i n t h e r a n g e 1 t o 1 0 .
I s y o u r n u m b e r L e s s t h a n , G r e a t e r t h a n , o r E q u a l t o 5 ?
T y p e ' L ' , ' G ' o r ' E ' : L
I s y o u r n u m b e r L e s s t h a n , G r e a t e r t h a n , o r E q u a l t o 2 ?
T y p e ' L ' , ' G ' o r ' E ' : g
I s y o u r n u m b e r L e s s t h a n , G r e a t e r t h a n , o r E q u a l t o 3 ?
T y p e ' L ' , ' G ' o r ' E ' : l
Y o u r a n s w e r s h a v e n o t b e e n c o n s i s t e n t .
$
Together these examples illustrate all the ways the game can end. Either the user acknowledges that the number was guessed by entering 'E', or the search space narrows to one number and the program announces this fact, or the program notices that the users responses are not logically consistent.
To get full credit your output should be formatted exactly as above, down to details like punctuation and pluralization of the word "guess" (i.e. "1 guess" vs. "2 guesses".) The final session below shows that it is possible for the program to end with no guesses.

$ p y t h o n Q u e s t i o n . p y
E n t e r t w o n u m b e r s , l o w t h e n h i g h .
l o w = 7
h i g h = 7
T h i n k o f a n u m b e r i n t h e r a n g e 7 t o 7 .
Y o u r n u m b e r i s 7 . I f o u n d i t i n 0 g u e s s e s .
$

More products