import random # Check that users have entered a valid option based on a list def string_checker(question, valid_ans): while True: error = f"Please enter a valid option from the following list: {valid_ans}" # 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 enter something that is valid print(error) print() # Displays instructions def instruction(): print(''' **** Instructions **** To begin, choose the number of rounds. Then answer the questions to the best of your ability Good Luck! ''') # checks for an integer more than 0 def int_check(question): while True: error = "Please enter an integer that is 1 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 for an integer between 5 and 20 def int_check_rounds(question): while True: error = "Please enter an integer between 5 and 20." to_check = input(question) try: response = int(to_check) # checks that the number is between 5 and 20 if response < 5 or response > 20: print(error) else: return response except ValueError: print(error) # compares user / random number and returns result (win / lose) def answer_compare(user, comp): # If the user and the computer answer is the same, it's a win if user == comp: round_result = "win" # if it's not a win then it's a loss else: round_result = "lose" return round_result # Initialise game variables mode = "regular" rounds_played = 0 rounds_lost = 0 rounds_won = 0 yes_no = ["yes", "no"] game_history = [] # Main Routine Starts here # Game Heading - game play starts here print("✖️✖️✖️️️ (っ◔◡◔)っ Multiplication Game ✖️✖️✖️") 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? ", yes_no) # checks users enter yes (y) or no (n) if want_instructions == "yes": instruction() # Ask user for number of rounds num_rounds = int_check_rounds("How many rounds would you like (between 5 and 20)? ") # Game loop starts here while rounds_played < num_rounds: # Generate two random integers between 1 and 12 random_integer = random.randint(1, 12) random_integer_two = random.randint(1, 12) # Multiplies the randoms and gives variable answer the answer answer = random_integer * random_integer_two # Rounds headings (based on mode) if mode == "regular": rounds_heading = f"\nRound {rounds_played + 1}" else: break print(rounds_heading) # Chooses the answer variable comp_choice = answer # get user choice user_choice = int_check(f"What is {random_integer} X {random_integer_two} ") # print("you chose", user_choice) result = answer_compare(user_choice, comp_choice) # Adjust game lost / game won if result == "lose": rounds_lost += 1 feedback = "😢😢 You lose. 😢😢" else: feedback = "👍👍 You won. 👍👍" # 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 {comp_choice}, {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 # Game History / Statistics area if rounds_played > 0: # Calculate Statistics 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 ") print("Thank you for playing")