import random # generates the two terms to use in questions def int_generator(): # Generates a random number between 2 and 12 term_1 = random.randint(2,12) term_2 = random.randint(2,12) return term_1, term_2 # generates a random operation for the quiz to use def ops_generator(): # The list of operators there is to choose from operators = ["+", "-", "*", "/"] # Randomizes the operator for the quiz op = random.choice(operators) return op # generates the question using the specific operator that is chosen from the # ops_gen function def question_generator(term_1, term_2, op): if op == "+": question = f"{term_1} + {term_2}" elif op == "-": question = f"{term_1} - {term_2}" elif op == "*": question = f"{term_1} * {term_2}" else: question = f"{term_1} / {term_2}" return question # Check that users have entered a valid # option based on a list def string_checker(question, valid_ans=("yes", "no")): error = f"Please enter a valid option from the following list: {valid_ans}" while True: # Get user response and make sure it's lowercase user_response = input(question).lower() for item in valid_ans: # check if the user response is a word in the list if item == user_response: return item # check if the user response is a word in the list # the first letter of an item in the list elif user_response == item[0]: return item # print error if user does not enter something that is valid print(error) print() # Displays instructions def instructions(): print(""" *** Instructions *** This is a basic arithmetic quiz. Each question will use two random numbers and a random operator (+, -, * or /). Your goal is to answer each question correctly. How it works: • Choose how many questions you want to answer • For each round, a new maths question will appear. • Type your answer and press . • The quiz will tell you if you are correct or incorrect. • If you entered an incorrect answer, the correct answer will be shown. • Your total score will be shown at the end if you wish to see them. Rules: • All numbers are between 2 and 12. • Division questions may produce decimal answers. • If an answer contains decimals it will be rounded to 1 dp. • You can exit the quiz at any time by typing 'xxx'. Good luck! """) # checks for an integer between 5 and 15 for round count def int_check(): while True: error = "Please enter an integer from 5 - 15: " try: response = int(input("How many rounds (5 - 15) would you like to play?\nYour answer: ")) # checks that the number is from the range of 5 - 15 if 5 <= response <= 15: return response else: print(error) except ValueError: print(error) # Main routine # starts game variables rounds_played = 0 correct_answers = 0 incorrect_answers = 0 # asks if user wants instructions print("Welcome to the maths quiz!") want_instructions = string_checker("Would you like to see the instructions on how to play?\nYour answer: ") print() # displays instructions if user requests them if want_instructions == "yes": instructions() # asks user how many rounds they want # and prints the chosen amount of rounds num_rounds = int_check() print(f"The chosen amount of rounds is {num_rounds}") # Game loop starts here while rounds_played < num_rounds: # Game variables term_1, term_2 = int_generator() op = ops_generator() question = question_generator(term_1, term_2, op) answer = round(eval(question), 1) rounds = rounds_played + 1 # Rounds heading print(f"\n___ Round {rounds} out of {num_rounds} ___") # Gets user input user_input = (input(f"What is {question}?:\nYour answer: ")).lower() # if user sends xxx ends the game if user_input == "xxx": break # turns user input into a float and checks if it is a number # if it isn't a number then add an incorrect answer and continue to # next round try: user_answer = round(float(user_input), 1) except ValueError: print("You entered an invalid answer. This will be marked incorrect.") incorrect_answers += 1 rounds_played += 1 continue # Checks if answer is correct if user_answer == answer: correct_answers += 1 print("Correct!") else: incorrect_answers += 1 print(f"Incorrect! The correct answer is {answer}.") # adds the current round played to the total rounds_played += 1 # End of game loop print("You have finished the quiz!") print() # game score calculations total = correct_answers + incorrect_answers correct_percent = (correct_answers / total) * 100 incorrect_percent = (incorrect_answers / total) * 100 see_score = string_checker("Would you like to see your score?\nYour answer: ") if see_score== "yes": print() print(f"You played {rounds_played} rounds!" f"\n With {correct_answers} ({correct_percent:.1f}%) correct answers" f"\n and {incorrect_answers} ({incorrect_percent:.1f}%) incorrect answers")