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 geometry_instruction(): print(""" ~~~ Geometry Quiz Instructions ~~~ ~~ How to Play: - Select the total number of rounds you want to play. - Each turn will randomly ask for either the Area or Perimeter of a Shape (Square or Rectangle). - Enter your numerical answer (no units required like cm² or m). - Type at any point to exit early. ~~ Number Ranges: - Area dimensions: 2 to 24 - Perimeter dimensions: 2 to 36 Good luck! """) # --- Main Program --- score = 0 answer_check = ["yes", "no"] # Check for instructions want_instruction = string_checker("Do you want to see the Geometry Quiz instructions? ", answer_check) if want_instruction == "yes": geometry_instruction() # Ask for total rounds rounds_input = input("\nHow many rounds would you like to play? ").strip().lower() while not rounds_input.isdigit() or int(rounds_input) < 1: print("⚠️ Please enter a valid number greater than 0. ⚠️") rounds_input = input("How many rounds would you like to play? ").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} ~~~") # Pick shape: Square (1) or Rectangle (2) shape_type = random.choice(["square", "rectangle"]) # Pick question type: Area (1) or Perimeter (2) calc_type = random.choice(["area", "perimeter"]) if shape_type == "square": if calc_type == "area": side = random.randint(2, 24) correct_ans = side * side prompt_msg = f"What is the area of a square with a side length of {side}?" else: # perimeter side = random.randint(2, 36) correct_ans = side * 4 prompt_msg = f"What is the perimeter of a square with a side length of {side}?" else: # rectangle if calc_type == "area": length = random.randint(2, 24) width = random.randint(2, 24) correct_ans = length * width prompt_msg = f"What is the area of a rectangle with length {length} and width {width}?" else: # perimeter length = random.randint(2, 36) width = random.randint(2, 36) correct_ans = (length + width) * 2 prompt_msg = f"What is the perimeter of a rectangle with length {length} and width {width}?" print(prompt_msg) user_input = input("Please enter your answer here or type 'xxx' to quit: ").strip().lower() # Exit code check if user_input == "xxx": print("\nThe game ended early.") break # Answer validation & scoring try: user_ans = int(user_input) if user_ans == correct_ans: print("Correct! Excellent job.") score += 1 else: print(f"Incorrect. The correct answer was {correct_ans}") except ValueError: print(f"Invalid input (skipped). The correct answer was {correct_ans}") # Always increment round count after each question turn question_num += 1 # Final Results rounds_played = question_num - 1 print("\n=========================") print("Geometry Quiz Finished") print(f"Your final score is {score}/{rounds_played}") print("=========================")