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 valid print(error) print() def instructions(): """Prints instructions""" print(""" *** Instructions *** To begin, choose the number of rounds. Then play the math game by simply answering the questions. The rules are as follows: Make sure to enter an Integer and not a letter or symbol. Good luck! """) # check that users have entered a valid def int_check(question): while True: error = "Please enter and integer that is 1 or more." to_check = input(question) # check for infinite mode if to_check == "": return "infinite" try: response = int(to_check) # checks that the number is more than / equal to 13 if response < 1: print(error) else: return response except ValueError: print(error) # Main routine Stars here # Intialise game variables mode = "regular" round_played = 0 round_lost = 0 rounds_won = 0 yes_no = ["yes","no"] print("πŸ€“πŸ’―πŸ“š Multiplication Math Quiz πŸ˜­πŸ“–βŒ") print() # ask the user if they want instructions (check they say yes / no) want_instructions = string_checker("Do you want to see the instructions? ", yes_no) # Display the instructions if the user wants to see them... if want_instructions == "yes": instructions() # Ask user for number of rounds / infinite mde num_rounds = int_check("How many rounds would you like?") if num_rounds == "infinite": mode = "infinite" num_rounds = 5 # Game loop starts here while round_played < num_rounds: # Rounds headings if mode == "infinite": rounds_heading = f"\n∞ ∞ ∞ Round {round_played + 1} (SECRET) 5 round Mode ∞ ∞ ∞" else: rounds_heading =f"\nπŸ’ΏπŸ’ΏπŸ’Ώ Round {round_played + 1} of {num_rounds} πŸ’ΏπŸ’ΏπŸ’Ώ" print(rounds_heading) # generate a random integer between 0 and 10 random_integer = random.randint(0, 10) random_integer2 = random.randint(0, 10) answer = random_integer * random_integer2 while True: try: user_choice = int(input(f"What is {random_integer} X {random_integer2}? ")) if user_choice == answer: print("πŸ‘πŸ‘ Correct! πŸ‘πŸ‘") error = 'false' round_played += 1 rounds_won += 1 else: error = 'false' print(f"Incorrect, correct answer: {answer}.") round_played += 1 round_lost += 1 except ValueError: error = 'true' print("Error! Please enter a valid integer") while error == 'true': try: user_choice = int(input(f"What is {random_integer} X {random_integer2}? ")) if user_choice == answer: print("πŸ‘πŸ‘ Correct! πŸ‘πŸ‘") error = 'false' round_played += 1 rounds_won += 1 else: error = 'false' print(f"Incorrect, correct answer: {answer}.") round_played += 1 round_lost += 1 except ValueError: error = 'true' print("Error! Please enter a valid integer") break # Calculate Statistics percent_won = rounds_won / round_played * 100 percent_lost = round_lost / round_played * 100 print('') # Output Game Statistics print("πŸ“ŠπŸ“ŠπŸ“Š Game Statistics πŸ“ŠπŸ“ŠπŸ“Š") print(f"πŸ‘ Won: {percent_won:.2f} \t " f"😒 Lost: {percent_lost:.2f} \t ") print('') print("Thanks for playing!")