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 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 your difficulty level, choose either easy (0-5 times table) or hard (6-12 times table). Then pick the number of rounds you would like to play (5-15 rounds only) The computer will give you a random multiplication problem and you have to answer it. Answer the questions in integers, (full numbers) only. Once finished you will receive your score! Good Luck!πŸ‘ """) # checks for an integer more than 5 and less than 15 def int_check(question): while True: invalid = "Invalid integer please try again!" to_check = input(question) try: response = int(to_check) # checks that the number is more than / equal to 1 if response < 5: print(invalid) elif response > 15: print(invalid) else: return response except ValueError: print(invalid) # Main Routine Starts here # Intialise game variables mode = "regular" rounds_played = 0 rounds_lost = 0 yes_no = ["yes","no"] difficulty_lvl = ["easy", "hard"] # Game Heading - game play starts here print("βœ–οΈπ™ˆπ™ͺπ™‘π™©π™žπ™₯π™‘π™žπ™˜π™–π™©π™žπ™€π™£ π™Œπ™ͺπ™žπ™―!βœ–οΈ") 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? ", yes_no) # checks users enter yes (y) or no (n) if yes prints instructions if want_instructions == "yes": instruction() #ask for difficulty setting difficulty = string_checker (" Choose your difficulty level (Easy or Hard) ", difficulty_lvl) #checks difficulty if difficulty == "easy": low_int = 0 high_int = 5 elif difficulty == "hard": low_int = 6 high_int = 12 print(f'You picked {difficulty}') # Ask user for number of rounds num_rounds = int_check("Please enter the number of rounds you would like to play! Integers between 5 and 15 only: ") # Game loop starts here while rounds_played < num_rounds: # Rounds headings rounds_heading = f"\nπŸ’ΏπŸ’ΏπŸ’Ώ Round {rounds_played + 1} of {num_rounds} πŸ’ΏπŸ’ΏπŸ’Ώ" print(rounds_heading) # Generate two random numbers random_integer_1 = random.randint(low_int,high_int) random_integer_2 = random.randint(low_int, high_int) # multiply the two random numbers and store the answer answer = random_integer_1 * random_integer_2 # compares the users input to the stored answer and outputs a response accordingly while True: try: user_choice = int( input(f"What is {random_integer_1} X {random_integer_2}? ")) if user_choice == answer: print("πŸ‘πŸ‘ Correct! πŸ‘πŸ‘") error = 'false' else: rounds_lost += 1 error = 'false' print(f"Incorrect, correct answer: {answer}.") except ValueError: error = 'true' print("❗Error! Please enter a valid integer❗") # In the instance of a value error re-run the code until the error is resolved while error == 'true': try: user_choice = int( input( f"What is {random_integer_1} X {random_integer_2}? ")) if user_choice == answer: print("πŸ‘πŸ‘ Correct! πŸ‘πŸ‘") error = 'false' else: error = 'false' rounds_lost += 1 print(f"Incorrect 😭, correct answer: {answer}.") except ValueError: error = 'true' print("❗Error! Please enter a valid integer❗") break # 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"πŸ‘ Correct: {percent_won:.2f}% \t " f"😒 Incorrect: {percent_lost:.2f}% \t ") print() print("Thanks for playing.")