#!/usr/bin/python3

import random

# 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 main game loop function
def play():

    # reference global variables
    global ans
    global tries

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

        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()))
        elif guess > ans:
            print("\t {0} is too {1}".format(guess,"high".capitalize()))
        else:
            print("You Win!!!")
            break

        tries += 1

    # outside loop
    if tries > 10:
        print("Sorry, you lose! the answer was: {0}".format(ans))
# end of play()


# main routine starts here
replay = 'Y'
while replay == 'Y':
    startup()
    play()

    response = input("Would you like to play again? (y/n) ")
    replay = response.strip().upper()[0]

print("goodbye")
# end of main game
