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 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 on the list elif user_response == item[0]: return item # print error if user does not enter something that is valid print(error) print() # Displays instructions def instructions(): print(''' ***Instructions*** To begin, choose the number of questions. You can play between 1 and 15. Then, answer the multiplication questions. Don't worry if you're not sure about an answer. Just give it a go! Press to end the game at anytime. Good Luck! ''') # checks for an integer more than 0 def int_check(question): while True: error = "Please enter an integer that is between 1 and 15." to_check = input(question) try: response = int(to_check) # checks that the number is between 1 and 15 if response < 1 or response > 15: print(error) else: return response except ValueError: print(error) # Main routine starts here # Initialise game variables mode = "regular" questions_answered = 0 correct_answers = 0 incorrect_answers = 0 print("🤓✏️❔ Multiplication Game 🤓✏️❔") print() # ask the user if they want instructions and display # them if requested want_instructions = string_checker("Do you want to see the instructions? ") # checks users enter yes (y) or no (n) if want_instructions == "yes": instructions() # Ask user for the number of questions num_questions = int_check("How many questions would you like? ") # Game loop starts here while questions_answered < num_questions: number_1 = random.randint(1, 12) number_2 = random.randint(1, 12) # Rounds headings (based on mode) questions_heading = f"\n⏱️⏱️⏱️ question {questions_answered + 1} of {num_questions} ⏱️⏱️⏱️" print(questions_heading) # Ask user a multiplication question user_answer = input(f"Question: {number_1} x {number_2} = ") print("You answered: ", user_answer) # Adjust questions answered questions_answered += 1 # If user answer is the exit code, break the loop if user_answer == "xxx": break