# Arup Guha
# 3/29/2020
# Arithmetic Game for COP 2930 Pair Program #2

import random

QUESTIONS_PER_ROUND = 10

# Runs the main program.
def main():

    # Accumulators.
    totalRounds = 0
    totalCorrect = 0

    ans = menu()

    # Keep going until the user quits.
    while ans != 5:

        # Add 1 to the rounds, and update the total correct.
        totalRounds += 1
        totalCorrect += game(ans)
        
        # Reprompt the menu.
        ans = menu()

    # Print total results.
    perc = totalCorrect/(QUESTIONS_PER_ROUND*totalRounds)*100
    print("You correctly solved",totalCorrect,"out of",QUESTIONS_PER_ROUND*totalRounds, end=" ")
    print("arithmetic problems, for a ",perc,"% success rate.", sep="")
    
# Returns the user's valid menu choice (1-5).
def menu():

    choice = 0

    # Go until there is a valid choice.
    while choice < 1 or choice > 5:

        # Present options.
        print("Please choose one of the following options.")
        print("1. Addition Game")
        print("2. Subtraction Game")
        print("3. Multiplication Game")
        print("4. Division Game")
        print("5. Quit")

        # Get user's choice.
        choice = int(input(""))

        # Error message.
        if choice < 1 or choice > 5:
            print("Sorry, that is an invalid option.")

    # This was the user's choice
    return choice

# Plays the game depending on which (1=add, 2=sub, 3=mult, 4=div)
def game(which):

    correct = 0

    # Ask all the round questions.
    for i in range(QUESTIONS_PER_ROUND):

        # Generate values.
        x = random.randint(1, 10)
        y = random.randint(1, 10)

        # Get the correct answer.
        ans = getAns(x, y, which)

        # Read in the user's answer.
        userans = int(input(getQuestion(x, y, which)))

        # They got it.
        if ans == userans:
            print("Great, you got it!")
            correct += 1

        # Missed it, print result.
        else:
            print("Sorry, that is incorrect, the correct answer is",ans)

    # Print round results.
    print("You got",correct,"questions correct.")
    print("That is ", correct/QUESTIONS_PER_ROUND*100,"% for this round.", sep="")

    # Need to return this.
    return correct

# Returns the answer of this adjusted query.
def getAns(x, y, which):

    # Addition is normal.
    if which == 1:
        return x + y

    # So is multiplication.
    elif which == 3:
        return x * y

    # For both subraction and division, we construct the question so the
    # answer is x.
    else:
        return x

# Returns the appropriate question using numbers x, y and operator which.
def getQuestion(x, y, which):

    # Set up the first operand.
    opOne = x
    if which == 2:
        opOne = x + y
    if which == 4:
        opOne = x * y

    # Set up operator.
    op = "+"
    if which == 2:
        op = "-"
    elif which == 3:
        op = "*"
    elif which == 4:
        op = "/"

    # Here is our question.
    return "What is "+str(opOne)+" "+op+" "+str(y)+"?\n"

# Do it!
main()
