#!/usr/bin/python3

import random

# define my global variables
ans = 0
tries = 0
high = 1000

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

    # reference global variables
    global ans
    global tries

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

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

    # start the game
    print('''Welcome to the Python Number Guessing Game
     please pick a number between 1 and 1000
     you have 10 guesses. Good Luck''')

# 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 ""
    else:
        return stuff.strip().upper()[0]
# end of letter()

# the main game loop function
def play():

    # reference global variables
    global ans
    global tries

    while tries <= 10:
        message = "\nGuess: {0:d} ".format(tries)
        reply = talk(message)

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

        if guess < ans:
            # note the end='', this means the print will not add a "newline"
            print("\t {0} is too {1}".format(guess,"low".capitalize()),end='')
        elif guess > ans:
            # note the end='', this means the print will not add a "newline"
            print("\t {0} is too {1}".format(guess,"high".capitalize()),end='')
        else:
            print( "You Win!!!")
            break

        tries += 1

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

    # outside loop
# end of play()

# 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())
         except:
            print("Sorry invalid input, High value = 1000")
            high = 1000;

print("Goodbye")
# end of main game
