from colorama import Fore
import sys

"""
declare all questions & answers within a dictionary
key: question
value: answer
"""
question_answers = {
  "x\u00b2+3x+2": "(x+1)(x+2)", 
  "x\u00b2+x-6": "(x-2)(x+3)", 
  "a\u00b2-2a-35": "(a+5)(a-7)", 
  "3y\u00b2-15y+18": "3(y-2)(y-3)",
}

question_number = 0

correct_answers = 0

"""
print with the formatted colors & prefix
@param string - the string printed
"""
def format_print( string: str ):
  print( Fore.BLUE + "\q\> " + Fore.WHITE + string + Fore.WHITE)

"""
format the input prompt with colors & prefix
@param string - the string printed
"""
def format_input ( string: str ):
  return input( Fore.BLUE + "\q\> " + Fore.WHITE + string + Fore.WHITE)

"""
check if user answer is the same as the question answer
@param user_answer - the answer provided by the user
@param question - the key used to access the answer in questions_and_answers
"""
def check_answer( user_answer: str, question: str ):

  if (user_answer == question_answers[question]):
    return True
  else:
    return False

######
# Begin script!!
######

continue_quiz = format_input( Fore.GREEN + "Hello There!" + Fore.WHITE + " Are you ready to take this 4 question quiz on factoring polynomials? (y/n) ")

# exit the script if prompted
if continue_quiz != "y": 
  format_print( Fore.RED + "L goodbye")
  sys.exit()

# print example question and response
format_print("Example Question #0: x\u00b2-16")
format_print("Example Answer: (x+4)(x-4)")

format_print( Fore.RED + "Remember not to add spaces!!" )

# wait for user to be fully ready
format_input("(Press enter to start the quiz) ")

format_print( Fore.GREEN + "Have Fun!!" )

# iterate through items in question_answers
for question, answer in question_answers.items():

  # increase the question_number by one since indices
  question_number += 1

  # print the current question
  format_print("~~ Question #" + str(question_number) + ": " + question)

  # store the user answer
  user_answer = format_input( Fore.YELLOW + "Your Answer: ")

  if (check_answer(user_answer=user_answer, question=question)):

    format_print( Fore.GREEN + "Correct!" + Fore.WHITE )

    # increase correct answers by one
    correct_answers += 1
  
  else:

    # print the correct answer if user is incorrect
    format_print( Fore.RED + "Stupid ahh")
    format_print("The correct answer is: " + Fore.GREEN + answer)

# print % of answers correct & leave
format_print( Fore.GREEN + "Quiz Complete! " + Fore.WHITE + "You got " + str(correct_answers/4*100) + r"% of answers correct :)")
\q\> Hello There! Are you ready to take this 4 question quiz on factoring polynomials? (y/n) y
\q\> Example Question #0: x²-16
\q\> Example Answer: (x+4)(x-4)
\q\> Remember not to add spaces!!
\q\> (Press enter to start the quiz) 
\q\> Have Fun!!
\q\> ~~ Question #1: x²+3x+2
\q\> Your Answer: (x+1)(x+2)
\q\> Correct!
\q\> ~~ Question #2: x²+x-6
\q\> Your Answer: (x-2)(x+3)
\q\> Correct!
\q\> ~~ Question #3: a²-2a-35
\q\> Your Answer: (a+5)(a-7)
\q\> Correct!
\q\> ~~ Question #4: 3y²-15y+18
\q\> Your Answer: (3y+3)(y-6)
\q\> Stupid ahh
\q\> The correct answer is: 3(y-2)(y-3)
\q\> Quiz Complete! You got 75.0% of answers correct :)