#!/usr/bin/python3

import random

# define my global variables
ans = 0
tries = 0
high = 1000
closestHighGuess = 1000
closestLowGuess = 1

# define my functions
# the startup function to initialize everything
def startup():

    # reference global variables
    global ans
    global tries
    global closestHighGuess
    global closestLowGuess

    # generate the random answer (1 - 1000)
    ans = random.randint(1,high)

    # initialize the counter to keep track of users tries
    tries = 1

    # initialize closest guesses, for hint
    closestLowGuess = 1
    closestHighGuess = high

    # start the game
    print('''Welcome to the Python Number Guessing Game
     please pick a number between 1 and {0}
     you have 10 guesses. Q = Quit, H = Hint
     Good Luck'''.format(high))
# end of startup()

# the talk to the user function
def talk(message):
    answer = input(message)
    return answer
# end of talk()

# the give me a single letter function
def letter(stuff):
    if len(stuff) == 0:
         return ""  # nothing passed to this function
    else:
        return stuff.strip().upper()[0]
# end of letter()

# the main game loop function
def play():

    # reference global variables
    global ans
    global tries
    global closestHighGuess
    global closestLowGuess

    while tries <= 10:
        message = "Guess: {} ".format(tries)
        reply = talk(message)

        if not reply.strip().isdigit():
            # check for Quit and Hint
            handled = checkSpecialRequest(reply)
            if handled:
                continue

        try:
            guess = int(reply.strip())
        except:
            print("Please enter a number")
            continue

        if guess < ans:
            print("\t {0} is too {1}".format(guess,"low".capitalize()))
            if guess > closestLowGuess:   # this means it is "closer"
                closestLowGuess = guess
        elif guess > ans:
            print("\t {0} is too {1}".format(guess,"high".capitalize()))
            if guess < closestHighGuess:   # this means it is "closer"
                closestHighGuess = guess
        else:
            print("You Win!!!")
            break

        tries += 1

    else:
        # comes here if the while loop did not get a "break" statement
        print("Sorry, you lose! the answer was: {0}".format(ans))

    # outside loop
# end of play()

# the handle special requests function
def checkSpecialRequest(incommingUserData):

    # reference global variables
    global ans
    global closestHighGuess
    global closestLowGuess

    # define my feedback to the routine that called me
    iTookCareOfIt = False

    firstLetter = letter(incommingUserData)
    if firstLetter == 'Q':
        print("Quiting, OK! the answer was: {0}".format(ans))
        quit()
        iTookCareOfIt = True

    if firstLetter == 'H':
        hint = ((closestHighGuess - closestLowGuess) // 2) + closestLowGuess
        print("I suggest you try: {0}".format(hint))
        iTookCareOfIt = True

    return iTookCareOfIt

# end of checkSpecialRequest()

# main routine starts here
while True:
    startup()
    play()

    response = talk("Would you like to play again? (y/n) ")
    if letter(response) != 'Y':
        break

    response = talk("Would you like to change the range? (y/n) ")
    if letter(response) == 'Y':
         response = talk("Please enter the new High value: ")
         try:
            high = int(response.strip())
            print(">>>>",high)
         except:
            print("Sorry invalid input, High value = 1000")
            high = 1000;

print("Goodbye")
# end of main game
