import random # Check that users have entered a valid answer # option based on list def string_checker(question): valid_ans = ('yes', 'no') error = f"Please enter a valid option from the following list: {valid_ans}" while True: # Get user response and make sure it's lowercase user_response = input(question).lower() for item in valid_ans: # check if the user response is a word in the list if item == user_response: return item # check if the user response is the same as # the first letter of an item in the list elif user_response == item[0]: return item # print error if user does not entre something that is valid print(error) print() # Displays instructions def instructions(): """Print instructions""" print(""" *** Instructions **** To begin, choose the number of rounds (you can play between 5 and 20 rounds) Then answer the multiplication question. Give it your best go! Your results will be published at the end of the game. Press to end game at anytime Good Luck! """) # checks for an integer between 5 and 20 def int_checker(question): while True: error = "Please enter an integer that between 5 and 20." to_check = input(question) try: response = int(to_check) # checks that the number is more than / equal to 1 if response < 5 or response > 20: print(error) else: return response except ValueError: print(error) # Main Routine Starts here # Initialise game variables mode = "regular" rounds_played = 0 correct_answers = 0 incorrect_answers = 0 # Game heading print("🔢✏️✖️ Welcome to the multiplication quiz! ✖️✏️🔢") print() # ask user if they want to see the instructions and display # them if requested want_instructions = string_checker("Do you want to see the instructions? ") # checks users enters yes (y) or no (n) if want_instructions == "yes": instructions() # Ask user for a number of rounds / infinite mode num_rounds = int_checker("How many rounds would you like to play (between 5 and 20)? ") # Game loop starts here while rounds_played < num_rounds: first_number = random.randint(3, 12) second_number = random.randint(3, 12) # Rounds headings rounds_heading = f"\n Round {rounds_played + 1} of {num_rounds} " print(rounds_heading) # Asks multiplication question user_answer = (input(f"Question: {first_number} x {second_number} = ")) print(f"You choose, {user_answer}") # If user choice is the exit code, break the loop if user_answer == "xxx": break rounds_played += 1