import random def string_checker(question, valid_ans=("yes", "no")): error = f"Please enter a valid option from the following list: {valid_ans}" while True: user_response = input(question).strip().lower() if not user_response: print(error + "\n") continue for item in valid_ans: if user_response == item or user_response == item[0]: return item print(error + "\n") def instruction(): print(""" ~~~ 𝐐𝐮𝐢𝐳 𝐈𝐧𝐬𝐭𝐫𝐮𝐜𝐭𝐢𝐨𝐧𝐬 ~~~ ~~ How to Play: - Select the total number of questions you want to play (up to 100). - A problem will appear on your screen each turn. Type your numeric answer and hit Enter. - You'll immediately see if your response was right or wrong. - Need to leave early? Type at any prompt to quit the game instantly. Good luck! """) # --- Main Program --- score = 0 answer_check = ["yes", "no"] # Check if user wants instructions want_instruction = string_checker("Do you want to see the instructions? ", answer_check) if want_instruction == "yes": instruction() # Ask for rounds with a boundary limit of 100 rounds_input = input("\nHow many rounds would you like to play (1-100)? ").strip().lower() # Loop until input is a digit AND falls within the 1-100 boundary limit while not rounds_input.isdigit() or not (1 <= int(rounds_input) <= 100): print("⚠️ Please enter a valid number between 1 and 100. ⚠️") rounds_input = input("How many rounds would you like to play (1-100)? ").strip().lower() total_rounds = int(rounds_input) question_num = 1 # Game Loop while question_num <= total_rounds: print(f"\n~~~ Question {question_num} of {total_rounds} ~~~") random_1 = random.randint(2, 15) random_2 = random.randint(2, 15) ans = random_1 * random_2 print(f"What is {random_1} x {random_2}?") user_input = input("Please enter your answer here or type 'xxx' to quit: ").strip().lower() if user_input == "xxx": print("\nThe game ended early.") break try: user_ans = int(user_input) if user_ans == ans: print("You got it!") score += 1 else: print(f"Incorrect. The correct answer was {ans}") except ValueError: print(f"Invalid input (skipped). The correct answer was {ans}") # Always increment round count after each question turn question_num += 1 # Final Results rounds_played = question_num - 1 print("\n=========================") print("Quiz Finished") print(f"Your final score is {score}/{rounds_played}") print("=========================")