import random # check hat 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 res[onse 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 that is valid print(error) print() # Inter checker function to validate the user input is a number and is within a number range def int_check(question, low, high): error = f'Please enter a number between {low} and {high}.' while True: try: response = int(input(question)) if response >= low and response <= high: return response else: print(error) except ValueError: print(error) # Variables difficulty_list = ["easy", "medium", "hard"] rounds_played = 0 rounds = int_check(f"How many rounds do you want? (Put in number of 3 to 10) ", 3, 10) print(f"Great you chose {rounds} rounds.") print() rounds_diff = string_checker("What difficulty do you want? (Easy (e), Medium (m), or Hard (h): ", difficulty_list) while rounds_played < rounds: if rounds_diff == "easy": num1 = random.randint(1, 10) num2 = random.randint(1, 10) elif rounds_diff == "medium": num1 = random.randint(4, 12) num2 = random.randint(4, 12) else: num1 = random.randint(6, 15) num2 = random.randint(6, 15) answer = num1 * num2 user_answer = int_check(f"What is {num1} x {num2}? ", 1, 100) if answer == user_answer: print("Woohoo! correct answer") else: print("Incorrect!") rounds_played += 1 print("The program has ended")