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:
-
Import the
randommodule
This will help create random numbers for math questions.import random -
Create a function named
mathQuiz()
This keeps your game organized and easy to call.def mathQuiz():
pass # we will add code inside soon! -
Add a score variable named
points
This stores the playerβs score. Start at 0.points = 0
print(f"Your points: {points}") -
Use a
whileloop
Repeat the quiz until the player reaches 10 points.while points < 10:
print(f"Your points: {points}") -
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} = ? ")
-
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}.") -
Show a winning message when the player reaches 10
points
print("π Congrats! You reached 10 points!")
print(f"π Your final score is: {points}") -
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()
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()