import random def run_quiz(): print("--- Welcome to the Multiplication Quiz! ---") # Get quiz settings from the user try: num_questions = int(input("How many questions would you like? ")) except ValueError: print("Invalid input. Defaulting to 5 questions.") num_questions = 5 score = 0 # Loop through the questions for i in range(1, num_questions + 1): # Generate two random numbers between 2 and 12 number1 = random.randint(2, 12) number2 = random.randint(2, 12) correct_answer = number1 * number2 print(f"\nQuestion {i}: What is {number1} x {number2}?") # Get the user's answer and validate it try: user_answer = int(input("Your answer: ")) except ValueError: print("That wasn't a number! Counting as incorrect.") user_answer = None # Check if the answer is correct if user_answer == correct_answer: print("Correct! 🎉") score += 1 else: print(f"Incorrect. The correct answer was {correct_answer}.") # Calculate results percentage = round((score / num_questions) * 100, 1) # Display final score print("\n--- Quiz Finished! ---") print(f"Your Score: {score}/{num_questions} ({percentage}%)")