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()