import random # Check that users have entered a valid answer # option based on 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 entre something that is valid print(error) print() # checks for an integer between high and low def int_checker(question, low, high): while True: error = f"Please enter an integer that is between {low} and {high}." to_check = input(question) try: response = int(to_check) # checks that the number is equal or more if low <= response <= high: return response else: print(error) except ValueError: print(error) yes_no = ["yes", "no"] difficulty_levels = ["easy", "medium", "hard"] # Ask user for difficulty level difficulty = string_checker("Chose a difficulty level from easy, medium, or hard: ", difficulty_levels) print(f"You chose, {difficulty}") # Game loop starts here for _ in range(5): # Set number ranges based on chosen difficulty if difficulty == "easy": first_number = random.randint(1, 5) second_number = random.randint(1, 5) elif difficulty == "medium": first_number = random.randint(3, 12) second_number = random.randint(3, 12) else: # hard first_number = random.randint(10, 20) second_number = random.randint(10, 20) # Asks multiplication question user_answer = int_checker(f"Question: {first_number} x {second_number} = ", 1, 400) print(f"You chose, {user_answer}")