import random # Check that users have entered a valid answer # option based on list def string_checker(question, valid_ans): error = "Please enter a valid option from the following list: ", yes_no 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 game statistics will be published at the end of the game. Good Luck! """) # checks for an integer between high and low def int_checker(question, low, high): while True: error = f"Please enter an integer that is between {low} and {high}." to_check = input(question) try: response = int(to_check) # checks that the number is equal or more if low <= response <= high: return response else: print(error) except ValueError: print(error) # Main Routine Starts here # Game heading print("\nšŸ”¢āœļøāœ–ļø Welcome to the multiplication quiz! āœ–ļøāœļøšŸ”¢") print() yes_no = ["yes", "no"] # 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? ", yes_no) # checks users enters yes (y) or no (n) if want_instructions == "yes": instructions() while True: # Initialise game variables mode = "regular" rounds_played = 0 answers_correct = 0 answers_incorrect = 0 percentage_correct = 0 game_history = [] # 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)? ", 5, 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 = int_checker(f"Question: {first_number} x {second_number} = ", 1, 144) print(f"You chose, {user_answer}") # Identifies the correct answer correct_answer = first_number * second_number # Display if answer is correct / incorrect and add results to game history if user_answer == correct_answer: answers_correct += 1 feedback ="āœ…āœ…āœ… Congratulations, that is the correct answer āœ…āœ…āœ…" else: answers_incorrect += 1 feedback = (f"āŒāŒāŒ Sorry, that was an incorrect answer āŒāŒāŒ. " f"\033[1mThe correct answer is {correct_answer} \033[0m") print(feedback) round_feedback = f"{first_number} x {second_number} = {user_answer}, {feedback}" history_item = f"Round: {rounds_played + 1} - {round_feedback}" game_history.append(history_item) rounds_played += 1 # Game loop ends here # Game history / Statistics area # Calculate percentage of correct answers percentage_correct = answers_correct / rounds_played * 100 # Output game statistics print("\nšŸ“ŠšŸ“ŠšŸ“Š Game Statistics šŸ“ŠšŸ“ŠšŸ“Š") print(f"āœ… Correct answers: {answers_correct} \t " f"āŒ Incorrect answers: {answers_incorrect} \t " f"šŸ‘ Percentage correct: {percentage_correct:.2f}%") # Ask if user wants game history, if yes display history print() history = string_checker("Do you want to see your game history? ", yes_no) if history == "yes": for item in game_history: print(item) # Allows users to replay entire game print() restart_game = string_checker("Would you like to play again? ", yes_no) if restart_game == "yes": continue else: break print("Thanks for playing!")