# A simple game where the computer tries to guess the number that a user is thinking of.

def yesNo( prompt ):
    """Ask the user the prompt, until the user replies with with a 'y' or a 'n' (upper or lowercase)"""
    response = ""
    while response != 'y' and response != 'Y' and response != 'n' and response != 'N':
        response = input( prompt )

    if response == 'y' or response == 'Y':
        answer = True
    else:
        answer = False
    return answer


# Outline:
# Welcome
# While the user wants to keep playing
#     While not the right answer
#         Cheating Check (if mid < low or mid > high)
#         Guess the middle of the valid range: mid = low + (high - low)/2
#         Ask if mid is the right answer
#         if not, ask if it's higher?
#             if it's higher, change low = mid + 1
#         otherwise, change high = mid - 1
#     Ask if user wants to play again
# Salutation


# Welcome
print("Welcome to the guessing game, where the computer tries to guess what number you're thinking of (0-100)")

playTheGame = True
while playTheGame:
    # new game
    rightAnswer = False
    low = 0
    high = 100
    while rightAnswer == False:
        # Guess the middle of the valid range
        mid = low + (high - low)//2

        if mid < low or mid > high:
            cheated = yesNo( "Wait a second, did you cheat? (Y/N) ")
            if cheated:
                print("Let me suggest some reading for you: https://www.acm.org/about-acm/code-of-ethics :)")
            else:
                print("Oh snap! Something went wrong.  Let's play again :)")
            break

        # Ask if mid is the right answer
        rightAnswer = yesNo( "Are you thinking of " + str(mid) + "? (Y/N) ")
        if rightAnswer == False:
            guessHigher = yesNo( "Are you thinking of a number higher than " + str(mid) + "? (Y/N) ")
            if guessHigher:
                # guess was too low, shift the lower bound up 
                low = mid + 1
            else:
                # guess was too high, shift the upper bound down
                high = mid - 1
    playTheGame = yesNo( "Great game!  Do you want to play again? (Y/N) ")

# Salutation
print("Bye!")