# Number guessing game, human guesses computer's random number
from random import randint  
from math import log   # natural log function

lower,upper = 1,1024
Gs = []  # records guesses in a list
limit = int(log(upper-lower+1)/log(2) + 1.5) # max guesses allowed
num = randint(lower,upper)
print("I have a number between",lower,"and",upper,"and you need to guess it.")
input("Press enter when ready...")
guess = num+1  # must have guess!=num to start loop
counter = 0 # counts attempts
while guess != num and counter<=limit:
    guess = int(input(("What's your guess? ")))
    counter += 1
    if not(guess in Gs):  # check if guess not already made
        Gs.append(guess)  # add guess to list of guesses
        if guess<lower or guess>upper:
            print("HA HA HA. That's ok, humans aren't perfect")
    else:
        print("Your memory is failing you. Try DDR4")
    if guess<num:
        print("The number is larger than your guess")
        if lower<=guess:     # lower should only increase
            lower = guess+1  # reset lower bound on reasonable guesses
    elif guess>num:          # upper should only decrease
        print("The number is smaller than your guess")
        if upper>=guess: upper = guess-1  # reset upper bound
#while
if counter<=limit:
    print("You guessed it in",counter,"tries.")
else: print("You exceeded the maximum number of guesses allowed. Nice try, human. That's ok.  Mother still loves you")

