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) # Checks for an integer more than 0 def int_equation_checker(question): while True: error = "Please enter an integer that is one or more" to_check = input(question) try: response = int(to_check) # checks that the number is more than / equal to 1 if response <1: print(error) else: return response except ValueError: print(error) # Checks if answer is correct / incorrect def answer_checker(): # Checks if answer is correct if user_answer == first_number * second_number: round_result = "correct" else: round_result = "incorrect" return round_result while True: # Main Routine Starts here # Initialise game variables mode = "regular" rounds_played = 0 answers_correct = 0 answers_incorrect = 0 game_history = [] # Game heading print("\nšŸ”¢āœļøāœ–ļø 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 = int_equation_checker(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 result = answer_checker() # Identifies the correct answer correct_answer = first_number * second_number # Display if answer is correct / incorrect and add results to game history if result == "correct": 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? ") if history == "yes": for item in game_history: print(item) print() restart_game = string_checker("Would you like to play again? ") if restart_game == "yes": continue else: break print("Thanks for playing!")