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 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 sam as # the first letter of an item in 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(): """Prints instructions""" print(""" *** Instructions **** Welcome to the multiplication game. The game will ask you how many rounds you would like. You will enter an integer between 1 and 10 and the computer will randomly generate an equation for you to solve. You are to solve the equation where the program will tell ou whether your answer is correct or incorrect. Try your best to get the answers right! Good Luck! """) # checks for an integer more than 0 (allows ) def int_check(question, low, high): while True: error = f"Please enter an integer from {low} to {high}." to_check = input(question) try: response = int(to_check) # checks that the number is more than / equal to 1 if low <= response <= high: return response else: print(error) except ValueError: print(error) # Main Routine Starts here # Intialise game variables rounds_played = 0 rounds_lost = 0 questions_incorrect = 0 questions_won = 0 yes_no = ["yes", "no"] game_history = [] # Ask user if they want to read instructions print("βŒβž—Multiplication Gameβž•βž–") print() want_instructions = string_checker("Do you want to read the instructions? ", yes_no) if want_instructions == "yes": instructions() # Ask user for number of rounds num_rounds = int_check("How many rounds would you like? (1-25): ", 1,25) # Game loop starts here while rounds_played < num_rounds: result = "lose" num_1 = random.randint(1, 10) num_2 = random.randint(1, 10) rounds_heading = f"\nπŸ’ΏπŸ’ΏπŸ’Ώ Round {rounds_played + 1} of {num_rounds}πŸ’ΏπŸ’ΏπŸ’Ώ" print(rounds_heading) # Get user choice user_answer = int_check(f"What does {num_1} x {num_2} = ? ", 1, 100) # target goal = int_check() # print(target_goal) # If user code is exit code, break the loop if user_answer ==(num_1*num_2): result = "win" # Adjust game lost to game history if result == "lose": rounds_lost += 1 feedback = "❌❌❌ That is the incorrect answer ❌❌❌" else: feedback = "βœ…βœ…βœ… That is the correct answer!!! βœ…βœ…βœ…" # Set up round feedback and output it user # Add it to the game history list (include the round number) round_feedback = f"The answer is: {num_1*num_2}, {feedback}" history_item = f"round: {rounds_played +1} - {round_feedback}" print(round_feedback) game_history.append(history_item) # increase number of rounds played. rounds_played += 1 # Game loop ends here # ask the user if they want instructions and display # them if requested # Game History / Statistics area if rounds_played > 0: # Calculate Statistics area rounds_won = rounds_played - rounds_lost percent_won = rounds_won / rounds_played * 100 percent_lost = rounds_lost / rounds_played * 100 # Output Game Statistics print("πŸ“ŠπŸ“ŠπŸ“Š Game Statistics πŸ“ŠπŸ“ŠπŸ“Š") print(f"πŸ‘ Won: {percent_won:.2f}% \t " f"😒 Lost. {percent_lost:.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) print() print("Thanks for playing.")