fredrik at pythonware.com
Thu Nov 10 18:40:41 CET 2005
Norman Silverstone wrote:
> > Heh, you will find that Python is practically executable pseudo-code!
> > Untested:
> > def guess_number():
> > # please don't cheat the poor computer...
> > print "Guess a number."
> > lo = 0
> > hi = 100
> > while True:
> > guess = (lo+hi)//2
> > ans = raw_input("Is it %d? y/n " % guess)
> > if ans in ('y', 'yes'):
> > break
> > ans = raw_input("Too high? y/n ")
> > if ans in ("y", "yes"):
> > hi = guess-1
> > else:
> > lo = guess+1
> > This should run, and it will *almost* do what you want.
> Thanks for that but I think it is too simplistic. It appears OK for the
> first guess, which is 50 but, what about the next guess.
did you test the script?
If the guess is
> too high then the next guess has to be 50/2. However, if it is too low
> then the next guess must be first guess + (100-second guess)/2. In general
> terms, if guess is too high then next guess must (guess - lowest
> possible)/2 and if too low then it is guess + (highest possible -
> Comments please.
More information about the Python-list