import random # Check that users have entered a valid option based on a list def string_checker(question, valid_ans): error = f"Please enter a valid answer 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 on the list elif user_response == item[0]: return item # print error if user does not enter something that is valid print(error) print() # Displays instructions def instructions(): print(''' ***Instructions*** To begin, choose the number of questions you would like to answer. You can play between 1 and 15. Then, answer the multiplication questions. Don't worry if you're not sure about an answer. Just give it a go! At the end of the game, your game statistics will be shown and you will have the option to see your history incase you would like to review your incorrect answers. Good Luck! ''') # checks for an integer between the high and low def int_check(question, low, high): while True: error = f"šŸ’” Hint šŸ’” Please enter an integer more than {low} and below {high}" to_check = input(question) try: response = int(to_check) # checks that the number is equal or between if low <= response <= high: return response else: print(error) except ValueError: print(error) # Main routine starts here # Initialise game variables questions_answered = 0 answers_correct = 0 answers_incorrect = 0 yes_no = ["yes", "no"] game_history = [] # Display game title and welcome user print("šŸ¤“āœļøā” Multiplication Game šŸ¤“āœļøā”") print() print("Welcome to this multiplication game!") print() # ask the user if they want instructions and display them if requested want_instructions = string_checker("Would you like to see the instructions? ", yes_no) # checks users enter yes (y) or no (n) if want_instructions == "yes": instructions() # Ask user for the number of questions num_questions = int_check("How many questions would you like? Please choose between 1 and 15: ", 1, 15) # Game loop starts here while questions_answered < num_questions: # Choose two random numbers between 1 and 12 number_1 = random.randint(1, 12) number_2 = random.randint(1, 12) # Generate and display question heading questions_heading = f"\nā±ļøā±ļøā±ļø Question {questions_answered + 1} of {num_questions} ā±ļøā±ļøā±ļø" print(questions_heading) # Ask user a multiplication question and accept answers between 1 and 200 user_answer = int_check(f"Question: {number_1} x {number_2} = ", 1, 200) print("You answered: ", user_answer) # Find the correct answer correct_answer = number_1 * number_2 # Check if user answer is correct/incorrect, adjust correct/incorrect counters, and generate user feedback if user_answer == correct_answer: answers_correct += 1 feedback = "Well done! That answer was correct āœ…" else: answers_incorrect += 1 feedback = f"Unlucky, that was incorrect āŒ. The correct answer is: {correct_answer}" # Output feedback, and add question and feedback to the game history list (include the question number) history_item = f"Question: {questions_answered +1} - {number_1} x {number_2} = {user_answer}, {feedback}" print(feedback) game_history.append(history_item) # Adjust questions answered questions_answered += 1 # Game History / Statistics area if questions_answered > 0: # Calculate Statistics answers_correct = questions_answered - answers_incorrect percent_correct = answers_correct / questions_answered * 100 percent_incorrect = answers_incorrect / questions_answered * 100 # Output Game Statistics print() print("šŸ“ŠšŸ“ŠšŸ“Š Game Statistics šŸ“ŠšŸ“ŠšŸ“Š") print(f"āœ… Correct: {percent_correct:.2f} \t " f"āŒ Incorrect: {percent_incorrect:.2f} \t") # Ask user if they want to see their game history and output it if requested see_history = string_checker("\nDo you want to see your game history? ", yes_no) if see_history == "yes": for item in game_history: print(item) # Game ends print() print("Thank you for playing!")