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 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 valid print(error) print() # Displays instructions def instructions(): # Prints instructions print(""" *** Instructions *** To begin, choose the number of rounds (or press for infinite mode). The rules are simple: The computer will ask you a multiplication question. Each number will be no higher than 12. All you have to do is enter the correct answer to the question! Type to end the game at any time. Good Luck! """) # Checks for an integer more than 0 (allows ) def int_check(question): while True: error = "Please enter an integer that is 1 or more." to_check = input(question) # check for infinite mode if to_check == "": return "infinite" elif to_check == "xxx": return "xxx" try: response = int(to_check) # check that the number is more than / equal to 1 if response < 1: print() print(error) print() else: return response except ValueError: print() print(error) print() # Compares user / computer choice and returns # result (win / lose / tie) def answer_compare(user, variable): # If the user and the actual answer is the same, it's correct if user == variable: round_result = "correct" # If it's not correct, then it's incorrect else: round_result = "incorrect" return # Checks what the user inputted and responds accordingly (exit code, error, etc...) # Main Routine starts here # Initialise game variables mode = "regular" rounds_played = 0 rounds_incorrect = 0 yes_no = ["yes", "no"] # random number 1 number = range(1, 12) for item in range(0, 11): ran_num = random.choice(number) # random number 2 number_two = range(1, 12) for item in range(0, 11): ran_num2 = random.choice(number_two) game_history = [] print() print(" o[]= Multiplication Game =[]o ") print() # Ask user if they want to see instructions, and display if requested want_instructions = string_checker("Do you want to read the instructions? ", yes_no) # Checks user enters yes (y) or no (n) if want_instructions == "yes": instructions() # Ask user for number of rounds / infinite mode num_rounds = int_check("How many rounds would you like? Push 'enter' for infinite mode ") if num_rounds == 'infinite': mode = "infinite" num_rounds = 5 # Game loop starts here while rounds_played < num_rounds: # Rounds headings if mode == "infinite": rounds_heading = f"\n.=+ Round {rounds_played + 1} (Infinite Mode) +=." else: rounds_heading = f"\n .=+ Round {rounds_played + 1} of {num_rounds} +=." print(rounds_heading) print() # Get question and answer ran_num = random.choice(number) ran_num2 = random.choice(number_two) multiply_question = int_check(f"What is {ran_num} multiplied by {ran_num2}? ") answer = ran_num * ran_num2 # Adjust game won / game lost counters and add results to game history if multiply_question == answer: feedback = f"You are correct!" # If user choice is the exit code, break the loop elif multiply_question == "xxx": break elif multiply_question != answer: rounds_incorrect += 1 feedback = f"You are incorrect. The answer was {answer}." # Set up round feedback and output it to user. # Add it to the game history list (include the round number) round_feedback = f"{ran_num} multiplied by {ran_num2}, {feedback}" history_item = f"Round: {rounds_played + 1} - {round_feedback}" print(feedback) game_history.append(history_item) rounds_played += 1 # if users are in infinite mode, increase number of rounds! if mode == "infinite": num_rounds += 1 # Game loop ends here print() # Game History / Statistics area if rounds_played > 0: # Calculate Statistics rounds_correct = rounds_played - rounds_incorrect percent_correct = rounds_correct / rounds_played * 100 percent_incorrect = rounds_incorrect / rounds_played * 100 # Output Game Statistics 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": print() for item in game_history: print(item) print() print("Thanks for playing.") else: print("Oops - You chickened out!")