most number guessing programs make you guess the number but heres one where the computer guesses ether random or the most efficient way.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | ```
#####################################################################
# program name: guess.py
# auther: max baseman
# email: dos.fool@gmail.com
# date: 5/01/07
# short description:
# this is a program that has two features
# one that randomly picks numbers till it gets yours
# and one that picks the most effective way till it gets your number
#####################################################################
from random import randrange as random
print "welcome to a number guessing program"
print
print "enter 1 for random "
print "or"
print "enter 2 for efficient"
print
guesstype=input(" >")
if guesstype == 1:
number=input("pick a number >")
numrange=input("pick a range >")+1
guessed=[0]
guess=random(numrange)
if guess==0:
guess=random(numrange)
print guess
guessed.append(guess)
guesses=1
while guess!=number:
guessed.append(guess)
guesses=guesses+1
guess=random(numrange)
if guess in guessed:
while guess in guessed:
guess=random(numrange)
print guess
print"i got the number",number,"in",guesses,"guesses, out of a range of",numrange-1
elif guesstype == 2:
number=input("pick a number >")
numrange=input("pick a range >")+1
guess=numrange/2
print guess
guesses=1
min=0
max=numrange
while guess!=number:
if guess < number:
min=guess
else:
max=guess
guess = (min+max) /2
guesses= guesses +1
print guess
print "i got the number",number,"in",guesses,"guesses, out of a range of",numrange-1
print
``` |

this program is mostly for fun maybe if someone was working in number theory or pasterns in randomness this might be useful.

A cleaner, more extensible version.