import random import time # Checks that users enters a number between a low and high range def int_check(question, low_number, high_number): while True: error = f"Please enter a number between {low_number} and {high_number}." try: response = int(input(question)) # checks that the number is more than / equal to 13 if response < low_number or response > high_number: print(error) else: return response except ValueError: print(error) def select_difficulty(level): """Allows the player to choose a difficulty level.""" if level == 1: return (1, 10) elif level == 2: return (2, 12) else: return (5, 20) def run_quiz(): print("=" * 40) print(" Welcome to the Multiplication Quiz!") print("=" * 40) while True: want_instructions = input("Do you want to see the instructions? ").lower() # check the user says yes / no if want_instructions == "yes" or want_instructions == "y": print("you said yes") break elif want_instructions == "no" or want_instructions == "n": print("you said no") break else: print("please enter yes / no") choice = int_check("Enter the difficulty (1-3): ",1,3) min_val, max_val = select_difficulty(choice) # Get the number of questions num_questions = int_check("How many questions would you like? ", 1, 10) score = 0 start_time = time.time() # Loop through questions for i in range(1, num_questions + 1): num1 = random.randint(min_val, max_val) num2 = random.randint(min_val, max_val) correct_answer = num1 * num2 user_answer = int_check(f"\nQuestion {i}/{num_questions}: What is {num1} × {num2}?",) # Get user's answer while True: try: user_answer = int(input("Your answer: ")) break except ValueError: print("Invalid input! Please enter a valid integer.") # Check answer if user_answer == correct_answer: print("✨ Correct!") score += 1 else: print(f"❌ Incorrect. The correct answer was {correct_answer}.") # Results calculation total_time = round(time.time() - start_time, 1) percentage = round((score / num_questions) * 100, 1) # Final summary print("\n" + "=" * 40) print(" QUIZ RESULTS ") print("=" * 40) print(f"Final Score : {score} out of {num_questions}") print(f"Accuracy : {percentage}%") print(f"Time Taken : {total_time} seconds") # Grade feedback if percentage == 100: print("Feedback : Perfect score! Outstanding job! 🎉") elif percentage >= 80: print("Feedback : Great work! You know your tables well. 👍") elif percentage >= 50: print("Feedback : Good attempt! Keep practicing. 💪") else: print("Feedback : Don't give up! Practice makes perfect. 📚") if __name__ == "__main__": run_quiz()