import random

# Define some sample quiz questions
questions = {
    "Math": [
        {"question": "What is 2 + 2?", "choices": ["a) 1", "b) 3", "c) 4"], "answer": "c)"},
        {"question": "What is the capital of France?", "choices": ["a) London", "b) Paris", "c) Berlin"], "answer": "b)"},
    ],
    "Science": [
        {"question": "What is the smallest unit of life?", "choices": ["a) Cell", "b) Atom", "c) Molecule"], "answer": "a)"},
        {"question": "What planet is furthest from the sun?", "choices": ["a) Earth", "b) Mars", "c) Neptune"], "answer": "c)"},
    ],
}

def ask_question(question, choices):
  """Prints the question and answer choices"""
  print(question)
  for choice in choices:
    print(choice)

def check_answer(user_answer, correct_answer):
  """Checks if the user's answer is correct and returns feedback"""
  if user_answer.lower() == correct_answer.lower():
    return "Correct!"
  else:
    return f"Incorrect. The correct answer is {correct_answer}"

def play_quiz(category, num_questions):
  """Manages the quiz flow for a chosen category and number of questions"""
  score = 0
  for _ in range(num_questions):
    # Select a random question from the chosen category
    question = random.choice(questions[category])
    ask_question(question["question"], question["choices"])
    user_answer = input("Enter your answer (a/b/c): ")
    feedback = check_answer(user_answer, question["answer"])
    print(feedback)
    if user_answer.lower() == question["answer"].lower():
      score += 1
  print(f"You scored {score} out of {num_questions} questions.")

# Main program loop
while True:
  # Display available categories
  print("Available categories:")
  for category in questions.keys():
    print(f"- {category}")

  # Get user input for category and number of questions
  category = input("Choose a category (or 'exit' to quit): ").capitalize()
  if category == "Exit":
    break
  if category not in questions:
    print("Invalid category. Please choose from the available options.")
    continue

  num_questions = int(input("Enter the number of questions: "))

  # Start the quiz
  play_quiz(category, num_questions)

  # Ask if user wants to play again
  play_again = input("Do you want to play again? (yes/no): ")
  if play_again.lower() != "yes":
    break

print("Thanks for playing!")