import random def int_check(min_value, max_value, prompt): """ Ask the user for an integer between min_value and max_value (inclusive). Accepts 'quit' to exit early (returns the string 'quit'). Keeps prompting until a valid integer is entered. """ while True: response = input(prompt).strip() if response.lower() == "quit": return "quit" if response.lstrip("-").isdigit(): value = int(response) if min_value <= value <= max_value: return value else: print(f"Please enter a number between {min_value} and {max_value}.\n") else: print("That is not a valid whole number. Type a number or 'quit' to stop.\n") def choose_difficulty(): """ Let the user choose difficulty using: e = easy m = medium h = hard Returns (name, max_value) """ while True: choice = input("Choose difficulty (easy(e), medium(m), hard(h)): ").strip().lower() if choice in ("e", "easy"): return "easy", 12 if choice in ("m", "medium"): return "medium", 15 if choice in ("h", "hard"): return "hard", 20 print("Invalid choice. Type e, m, or h.\n") def ask_question_count(): """ Ask how many questions the user wants. Must be between 1 and 10. """ while True: response = input("How many questions would you like? (1 to 10): ").strip() if response.lower() == "quit": return "quit" if response.isdigit(): num = int(response) if 1 <= num <= 10: return num else: print("Please enter a number between 1 and 10.\n") else: print("That is not a valid whole number. Please enter a number between 1 to 10 or type 'quit' to exit.\n") def show_instructions(): """ Display quiz instructions. """ print("\n========== INSTRUCTIONS ==========") print("• Choose a difficulty: easy, medium, or hard.") print("• You will get random multiplication questions.") print("• Type your answer as a whole number.") print("• Type 'quit' anytime to exit the quiz.") print("• At the end, you can view your question history.") print("===================================\n") def run_quiz(): print("====================================") print(" MULTIPLICATION QUIZ ") print("====================================\n") print("Welcome to the Multiplication Quiz!") print("You will be given random multiplication questions based on the difficulty you choose.") print("Try to get as many correct as you can.") print("Type 'quit' at any time to exit.\n") # NEW: Ask if user wants instructions while True: see_instructions = input("Do you want to see the instructions? (yes(y)/no(n)): ").strip().lower() if see_instructions in ("y", "yes"): show_instructions() break elif see_instructions in ("n", "no"): print("Skipping instructions.\n") break else: print("Invalid option. Please type yes(y) or no(n).\n") difficulty, max_rand = choose_difficulty() question_num = ask_question_count() if question_num == "quit": print("No questions selected. Exiting.") return rounds_played = 0 score = 0 min_answer = 2 max_answer = 400 history = [] # store question history while rounds_played < question_num: print(f"\n--- Question {rounds_played + 1} of {question_num} ---") random_1 = random.randint(2, max_rand) random_2 = random.randint(2, max_rand) ans = random_1 * random_2 user_ans = int_check(min_answer, max_answer, f"What is {random_1} x {random_2} = ") if user_ans == "quit": print("\nYou chose to quit the quiz early.") break correct = (user_ans == ans) if correct: print("You got it!") score += 1 else: print(f"The correct answer was {ans}") # Save history entry history.append({ "q": f"{random_1} x {random_2}", "your_answer": user_ans, "correct_answer": ans, "result": "Correct" if correct else "Wrong" }) rounds_played += 1 # FIXED: No division by zero crash if rounds_played > 0: accuracy = (score / rounds_played) * 100 else: accuracy = 0 print("\n====================================") print(" SUMMARY ") print("====================================") print(f"Difficulty: {difficulty}") print(f"Questions attempted: {rounds_played}") print(f"Score: {score}/{rounds_played}") print(f"Accuracy: {accuracy:.1f}%") print("====================================\n") # Ask if user wants to see history while True: show_history = input("Do you want to see your question history? (yes(y)/no(n)): ").strip().lower() if show_history in ("y", "yes"): print("\n========== QUESTION HISTORY ==========") for i, entry in enumerate(history, start=1): print(f"\nQuestion {i}: {entry['q']}") print(f"Your answer: {entry['your_answer']}") print(f"Correct answer: {entry['correct_answer']}") print(f"Result: {entry['result']}") print("\n======================================\n") break elif show_history in ("n", "no"): print("History skipped.\n") break else: print("Invalid option. Please type yes(y) or no(n).\n") if __name__ == "__main__": run_quiz() input("Press Enter to exit...")