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 following list: {valid_ans}" while True: user_response = input(question).lower() for item in valid_ans: if item == user_response: return item elif user_response == item[0]: return item # print error if user does not enter something that valid print(error) print() # Displays instructions def instruction(): print(''' ****Instructions**** You will see simple math questions like: > 2 x 5 = ? Type your answer in the space provided. Example: > If the question is 2 x 5 = ?, the correct answer is 10. If you answer correctly, you move on to the next question. ✔ Correct: 2 x 5 = 10 → Great job! If you answer incorrectly, don’t worry! ✘ Wrong: 2 x 5 = 12 → That’s not right. The correct answer will be shown so you can learn. Try to get as many correct as you can! Good Luck! ''') # Main routine print("🧠📝💡Multiplication Quiz🧠📝💡") 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? ") # checks users enter yes (y) or no (n) if want_instructions == "yes": instruction() # Game logic (multiplication quiz) def answer_compare(user, ans): if user == ans: return "correct" else: return "incorrect" questions_won = 0 questions_incorrect = 0 num_questions = 10 for i in range(num_questions): num_1 = random.randint(1, 9) num_2 = random.randint(1, 9) correct_answer = num_1 * num_2 try: user_answer = int(input(f'Question {i+1}: What is {num_1} x {num_2}? ')) except ValueError: print("Invalid input! Please enter a number.") questions_incorrect += 1 continue result = answer_compare(user_answer, correct_answer) if result == "correct": questions_won += 1 print("Correct!") else: questions_incorrect += 1 print(f"Wrong! The correct answer was {correct_answer}") # Final Score Summary print(f"\nGame Over! ✔ You got {questions_won} correct and ✘ {questions_incorrect} incorrect.")