Newb ??

Steven D'Aprano steve at
Thu Nov 10 15:39:40 CET 2005

On Thu, 10 Nov 2005 13:30:05 +0000, Norman Silverstone wrote:

>> In that case, think of "bisection".  Originally, all the computer knows
>> is that the number is in some range, say 0 to 100.  It can then guess
>> the midpoint, 50.  If it's right, yay!  Otherwise: if it's told to go
>> lower, then the range is now 0 to 49 -- if higher, it's 51 to 100; in
>> each case the range was just halved (actually, a bit more than halved).
> Thank you, I thought that might be the case. So, I will have to settle
> down and try to write some pseudo-code first. I hope to be back.

Heh, you will find that Python is practically executable pseudo-code!


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'):
        ans = raw_input("Too high? y/n ")
        if ans in ("y", "yes"):
            hi = guess-1
            lo = guess+1

This should run, and it will *almost* do what you want.


More information about the Python-list mailing list