import random
import time
class MathTestGame:
def __init__(self):
self.score = 0
self.total_questions = 5
self.start_time = 0
self.end_time = 0
def ask_question(self):
operations = ['+', '-', '*', '/']
operation = random.choice(operations)
if operation == '+':
num1 = random.randint(1, 50)
num2 = random.randint(1, 50)
answer = num1 + num2
question = f"{num1} + {num2} = ?"
elif operation == '-':
num1 = random.randint(1, 50)
num2 = random.randint(1, num1) # Ensure no negative results
answer = num1 - num2
question = f"{num1} - {num2} = ?"
elif operation == '*':
num1 = random.randint(1, 12)
num2 = random.randint(1, 12)
answer = num1 * num2
question = f"{num1} * {num2} = ?"
elif operation == '/':
num2 = random.randint(1, 12)
answer = random.randint(1, 12)
num1 = answer * num2
question = f"{num1} / {num2} = ?"
return question, answer
def start_game(self):
print("Welcome to the Math Test Game!")
print(f"You will answer {self.total_questions} questions. Let's begin!")
self.start_time = time.time()
for i in range(self.total_questions):
question, correct_answer = self.ask_question()
print(f"Question {i + 1}: {question}")
# Adding multiple-choice options
options = [correct_answer]
while len(options) < 4:
wrong_answer = random.randint(1, 100)
if wrong_answer not in options:
options.append(wrong_answer)
random.shuffle(options)
# Displaying options
for j, option in enumerate(options):
print(f"{j + 1}. {option}")
user_answer = input("Select the correct option (1-4): ")
# Check answer
if options[int(user_answer) - 1] == correct_answer:
print("Correct!\n")
self.score += 1
else:
print(f"Wrong! The correct answer was {correct_answer}.\n")
self.end_time = time.time()
self.display_results()
def display_results(self):
elapsed_time = self.end_time - self.start_time
print("Game Over!")
print(f"Your score: {self.score}/{self.total_questions}")
print(f"Time taken: {elapsed_time:.2f} seconds")
if self.score == self.total_questions:
print("Excellent work! You're a math whiz!")
elif self.score >= self.total_questions // 2:
print("Good job! Keep practicing!")
else:
print("Don't worry! Practice makes perfect.")
if __name__ == "__main__":
game = MathTestGame()
game.start_game()
Welcome to the Math Test Game!
You will answer 5 questions. Let's begin!
Question 1: 48 / 8 = ?
1. 77
2. 64
3. 63
4. 6
Wrong! The correct answer was 6.
Question 2: 30 / 6 = ?
1. 5
2. 26
3. 71
4. 84
Correct!
Question 3: 44 / 11 = ?
1. 4
2. 5
3. 54
4. 66
Correct!
Question 4: 11 / 11 = ?
1. 75
2. 38
3. 57
4. 1
Wrong! The correct answer was 1.
Question 5: 44 - 43 = ?
1. 98
2. 23
3. 8
4. 1
Correct!
Game Over!
Your score: 3/5
Time taken: 30.37 seconds
Good job! Keep practicing!