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" # allow negative numbers if needed; here we expect positive so use isdigit 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. Returns a tuple (name, max_value). Ranges match your original code: easy -> randint(2, 12) medium -> randint(2, 15) hard -> randint(2, 20) """ while True: choice = input("Select difficulty (easy / medium / hard): ").strip().lower() if choice == "easy": return "easy", 12 if choice == "medium": return "medium", 15 if choice == "hard": return "hard", 20 print("Invalid choice. Please type easy, medium, or hard.\n") def ask_question_count(): """ Ask how many questions the user wants. Minimum 1, maximum 1000 (arbitrary cap). """ while True: response = input("How many questions would you like? (type a whole number): ").strip() if response.lower() == "quit": return "quit" if response.isdigit(): num = int(response) if num > 0: return num else: print("Please enter a number greater than 0.\n") else: print("That is not a valid whole number. Try again or type 'quit' to exit.\n") def run_quiz(): global random_2, random_1 print("====================================") print(" MULTIPLICATION QUIZ ") print("====================================\n") print("Type 'quit' at any prompt to exit early.\n") difficulty, max_rand = choose_difficulty() question_num = ask_question_count() if question_num == "quit": print("No questions selected. Exiting.") return # initialize counters rounds_played = 0 score = 0 # main loop (keeps your original structure) while rounds_played < question_num: print(f"\n--- Question {rounds_played + 1} of {question_num} ") if difficulty == "easy": random_1 = random.randint(2, 12) random_2 = random.randint(2, 12) if difficulty == "medium": random_1 = random.randint(2, 15) random_2 = random.randint(2, 15) if difficulty == "hard": random_1 = random.randint(2, 20) random_2 = random.randint(2, 20) ans = random_1 * random_2 # use int_check with a reasonable allowed range for answers # smallest possible product is 2*2=4, largest depends on difficulty (12*12=144, 15*15=225, 20*20=400) user_ans = int_check(4, 400, f"What is {random_1} x {random_2} = ") if user_ans == "quit": print("\nYou chose to quit the quiz early.") break if user_ans == ans: print("You got it!") score += 1 else: print(f"The correct answer was {ans}") rounds_played = rounds_played + 1 # summary print("\n====================================") print(" SUMMARY ") print("====================================") print(f"Difficulty: {difficulty}") print(f"Questions attempted: {rounds_played}") print(f"Score: {score}/{rounds_played}") print("====================================\n") if __name__ == "__main__": run_quiz()