This is homework so no spoilers please!
The goal of the assignment is to create a program to guess your secret number from 0(inclusive)-100(not inclusive). I have made some headway, but I am having trouble creating a proper iteration that will collect the response and then based on the three possible scenarios of 'h' - high guess, 'l' - low guess (lowercase L) or c - correct guess do three different tasks.
If the guess is 'h' or 'l' I would need to iteratively guess by bisection search.
It's supposed to look like this in practice:
Is your secret number 91?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. y
Sorry, I did not understand your input.
Is your secret number 91?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. c
This is the code that I have produced so far - note the variables have not been cleaned up - as I started writing I tried one method and used some variables and tested other methods so the variables have grown.
As you can see I am having trouble with setting up the iteration. And handling the data structure. And passing the data back for processing... How should I approach this? Point me in the correct direction if you can or do I need to start from the ground up?
Thanks!!
The goal of the assignment is to create a program to guess your secret number from 0(inclusive)-100(not inclusive). I have made some headway, but I am having trouble creating a proper iteration that will collect the response and then based on the three possible scenarios of 'h' - high guess, 'l' - low guess (lowercase L) or c - correct guess do three different tasks.
If the guess is 'h' or 'l' I would need to iteratively guess by bisection search.
It's supposed to look like this in practice:
Is your secret number 91?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. y
Sorry, I did not understand your input.
Is your secret number 91?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. c
This is the code that I have produced so far - note the variables have not been cleaned up - as I started writing I tried one method and used some variables and tested other methods so the variables have grown.
Code:
choice = 0
guess = 50
win = 0
y = 100
hi = y
x = 0
lo = x
print 'Please think of a number between 0 and 100!'
print 'Is your secret number 50?'
while (win != 1):
choice = raw_input('Enter "h" to indicate the guess is too high. Enter "l" to indicate the guess is too low. Enter "c" /
to indicate I guessed correctly. ')
if choice == 'h':
guess = ((guess+lo)/2)+1
print guess
if choice == 'l':
guess = ((guess+hi)/2)-1
print guess
if choice == 'c':
print 'Game over. Your secret number was: ', guess
win += 1
break
if choice != 'h' or 'l' or 'c':
print 'Sorry, I did not understand your input.'
As you can see I am having trouble with setting up the iteration. And handling the data structure. And passing the data back for processing... How should I approach this? Point me in the correct direction if you can or do I need to start from the ground up?
Thanks!!