import random # Check if the user entered a valid option from the list def string_checker(question, valid_ans=("yes", "no")): error = f"Please enter a valid option from the following list: {valid_ans}" while True: user_response = input(question).lower() for item in valid_ans: if item == user_response or user_response == item[0]: return item print(error) print() # Checks for an integer more than 0 def int_check(question): while True: error = "Please enter an integer that is 1 or more." to_check = input(question) try: response = int(to_check) if response < 1: print(error) else: return response except ValueError: print(error) # Main Routine print("--- Simple Multiplication Quiz ---") print() want_instructions = string_checker("Do you want to see the instructions? ") if want_instructions == "yes": print("Welcome to my multiplication quiz.") print("You will be asked to multiply two random numbers.") print("Type 'xxx' as your answer to quit the game.") print() rounds_played = 0 score = 0 num_rounds = int_check("How many rounds do you want to play? ") # Game loop while rounds_played < num_rounds: rounds_heading = f"\n--- Question {rounds_played + 1} of {num_rounds} ---" print(rounds_heading) print() # Generate numbers between 5-15 and 2-10 number_one = random.randint(5, 15) number_two = random.randint(2, 10) answer = number_one * number_two # Get user answer user_input = input(f"{number_one} * {number_two} = ").lower() # Exit code check if user_input == "xxx": break # Checks if answer is correct try: user_answer = int(user_input) if user_answer == answer: print("Correct!") score += 1 else: print(f"Incorrect. The answer was {answer}") rounds_played += 1 except ValueError: print("Please enter a valid number or 'xxx' to quit.") # Game History and Statistics print("\n--- Game Over! ---") if rounds_played > 0: print(f"You scored {score} out of {rounds_played} questions correctly.") else: print("You didn't play any rounds!")