import random # Check that users have entered a valid # option based on a 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 var_item in valid_ans: # check if the user response is a word in the list if var_item == user_response: return var_item # check if the user response is the same as # the first letter of an item in the list elif user_response == var_item[0]: return var_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 play against the computer. Press to end the game at anytime. Good Luck! ''') # checks for an integer more than 0 (allows ) def int_check(question, low, high): while True: error = f"Please enter an integer that 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) # compares user / computer choice and returns # result (win / lose / tie) # Main Routine Starts here # Intialise game variables rounds_played = 0 rounds_lost = 0 game_history = [] # Game Heading - game play starts here print("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 read the instructions? ") # checks users enter yes (y) or no (n) if want_instructions == "yes": instruction() # Ask user for number of rounds num_rounds = int_check("How many rounds would you like? (1-20): ",1,20) # Game loop starts here while rounds_played < num_rounds: result = "lose" number_1 = random.randint(1,12) number_2 = random.randint(1,12) rounds_heading = f"\n💿💿💿 Round {rounds_played + 1} of {num_rounds} 💿💿💿" print(rounds_heading) # get user choice user_choice = int_check(f"What does {number_1}*{number_2}=? ",1,400) if user_choice == (number_1*number_2): result = "win" # Adjust game lost to game win if result == "loose": rounds_lost += 1 feedback = "You suck you got it wrong" else: feedback = "Good job you nerd it the right 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 : {number_1*number_2}, {feedback}" history_item = f"Round: {rounds_played + 1} - {round_feedback}" print(round_feedback) game_history.append(history_item) rounds_played += 1 # 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" Correct: {percent_won: .2f}% \t " f" Incorrect: {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? ") if see_history == "yes": print(item) print() print("Thanks for playing you actual nerd!!!!")