[ create a new paste ] login | about

Link: http://codepad.org/rtCqSkwx    [ raw code | fork ]

Python, pasted on Feb 11:
#!/usr/bin/env python

import sys

def do_guess(guess, high, low, tries):
    ri = raw_input("Is your number %02d? (y)es, (h)igher, (l)ower: " % guess)
    c = ri[0].lower()
    if c == "y":
        print "Wooho!  Guessed it in %d tries!" % tries
        sys.exit()
    else:
        tries += 1
        n = (high - low) / 2
        if not n:
            n = 1
        if c == "h":
            low = guess
            high = guess = guess + n
        elif c == "l":
            high = guess
            low = guess = guess - n
        do_guess(guess, high, low, tries)

def game():
    guess = 50
    high = guess
    low = 0
    tries = 1
    print "Think of a number between 1 and 100, when you are ready, press enter!"
    sys.stdin.read(1)
    do_guess(guess, high, low, tries)

if __name__ == "__main__":
    game()


Create a new paste based on this one


Comments: