Math Quiz

Python Exercises

๐ŸŽฏ Math Quiz Challenge!
Letโ€™s make a fun Math Quiz Game using a while loop in Python. Your goal is to reach 10 points by answering math questions correctly!

๐Ÿงฉ Step-by-Step Instructions:

  1. Import the random module
    This will help create random numbers for math questions.
    import random
  2. Create a function named mathQuiz()
    This keeps your game organized and easy to call.
    def mathQuiz():
      pass # we will add code inside soon!
  3. Add a score variable named points
    This stores the playerโ€™s score. Start at 0.
    points = 0
    print(f"Your points: {points}")
  4. Use a while loop
    Repeat the quiz until the player reaches 10 points.
    while points < 10:
      print(f"Your points: {points}")
  5. Create random numbers for your question
    Ask the player to solve the math problem.
    num1 = random.randint(1, 100)
    num2 = random.randint(1, 100)
    answer = input(f"{num1} + {num2} = ? ")
  1. Check if the answer is correct
    Increase points when correct, otherwise show the correct answer.
    if answer.isdigit() and int(answer) == num1 + num2:
      points += 1
      print("โœ… Correct!")
    else:
      print(f"โŒ Wrong! The answer was {num1 + num2}.")
  2. Show a winning message when the player reaches 10 points
    print("๐ŸŽ‰ Congrats! You reached 10 points!")
    print(f"๐Ÿ† Your final score is: {points}")
  3. Call your function to start the quiz
    mathQuiz()

๐Ÿ’ช Extra Challenge:

import random

def mathQuiz():
  points = 0

  print("๐ŸŽฏ Welcome to the Math Quiz!")
  print("Answer the addition problems correctly to earn points.")
  print("Get 10 points to win the game!\n")

  while points < 10:
    print(f"Your points: {points}")

    # Generate random numbers
    num1 = random.randint(1, 100)
    num2 = random.randint(1, 100)

    # Ask question
    answer = input(f"{num1} + {num2} = ? Your answer: ")

    # Validate answer
    if answer.isdigit() and int(answer) == num1 + num2:
      points += 1
      print("โœ… Correct! You earned 1 point.\n")
    else:
      print(f"โŒ Wrong! The correct answer was {num1 + num2}.\n")

  print("๐ŸŽ‰ Congrats! You reached 10 points!")
  print(f"๐Ÿ† Your final score is: {points}")

# Start the quiz
mathQuiz()