import random # 1. Simple helper function for Yes/No questions def ask_yes_no(prompt): while True: answer = input(prompt).lower().strip() if answer == "yes" or answer == "y": return True elif answer == "no" or answer == "n": return False else: print("Please enter yes / no") # 2. Main Game Function def play_quiz(): print("\n--- Welcome to the Geometry Quiz Quest! ---") # Ask how many questions (5 to 15) while True: user_choice = input("How many questions would you like to answer? (5 - 15): ").strip() if user_choice.isdigit(): total_questions = int(user_choice) if total_questions < 5: print("Please enter a number that is at least 5.") elif total_questions > 15: print("Please enter a number that is 15 or less.") else: print(f"Awesome! Starting a quiz with {total_questions} questions.\n") break else: print("Invalid input! Please enter a whole number.") score = 0 shapes = ["square", "rectangle"] quiz_types = ["area", "perimeter"] # Quiz Loop for i in range(total_questions): print(f"\nQuestion {i + 1} of {total_questions}:") # Pick a random shape and calculation type chosen_shape = random.choice(shapes) chosen_type = random.choice(quiz_types) # SQUARE QUESTIONS if chosen_shape == "square": side = random.randint(2, 12) if chosen_type == "area": print(f"A square has a side length of {side} cm.") print("What is its AREA in square cm?") correct_answer = side * side else: print(f"A square has a side length of {side} cm.") print("What is its PERIMETER in cm?") correct_answer = 4 * side # RECTANGLE QUESTIONS else: length = random.randint(3, 12) width = random.randint(2, 10) if chosen_type == "area": print(f"A rectangle has a length of {length} cm and width of {width} cm.") print("What is its AREA in square cm?") correct_answer = length * width else: print(f"A rectangle has a length of {length} cm and width of {width} cm.") print("What is its PERIMETER in cm?") correct_answer = 2 * (length + width) # Answer Input Validation Loop while True: user_input = input("Your answer: ") cleaned_input = user_input.lower().replace("cm", "").strip() if cleaned_input.isdigit(): user_answer = int(cleaned_input) break else: print("Invalid! Please enter a whole number.") # Check Answer if user_answer == correct_answer: print("Correct ✅") score += 1 else: print(f"Incorrect ❌ The correct answer was {correct_answer}.") # Show Final Score print("\n--- Quiz Finished! ---") print(f"Your final score is {score} out of {total_questions}.") # --- MAIN PROGRAM LOOP --- # Runs the quiz and uses ask_yes_no() to see if the user wants to play again playing = True while playing: play_quiz() playing = ask_yes_no("\nDo you want to play again? (yes/no): ") print("Thanks for playing! Goodbye.")