Spaces:
Runtime error
Runtime error
File size: 2,888 Bytes
4562618 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import random
from dataclasses import dataclass
@dataclass
class Problem:
num1: int
num2: int
operator: str
answer: int
def __str__(self):
return f"{self.num1} {self.operator} {self.num2} = ?"
def generate_problem(grade: int) -> Problem:
"""Generate a math problem appropriate for the given grade level"""
if grade == 1:
# Grade 1: Simple addition and subtraction with numbers 1-10
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
operator = random.choice(['+', '-'])
# Ensure subtraction doesn't result in negative numbers
if operator == '-' and num2 > num1:
num1, num2 = num2, num1
elif grade == 2:
# Grade 2: Addition and subtraction with numbers 1-20
num1 = random.randint(1, 20)
num2 = random.randint(1, 20)
operator = random.choice(['+', '-'])
if operator == '-' and num2 > num1:
num1, num2 = num2, num1
elif grade == 3:
# Grade 3: Addition, subtraction with numbers 1-50, and simple multiplication
num1 = random.randint(1, 50)
operator = random.choice(['+', '-', '*'])
if operator == '*':
num2 = random.randint(1, 10)
else:
num2 = random.randint(1, 50)
if operator == '-' and num2 > num1:
num1, num2 = num2, num1
elif grade == 4:
# Grade 4: All operations, multiplication with larger numbers
num1 = random.randint(1, 100)
operator = random.choice(['+', '-', '*', '/'])
if operator == '*':
num2 = random.randint(1, 12)
elif operator == '/':
# Generate division problems with whole number answers
num2 = random.randint(1, 12)
num1 = num2 * random.randint(1, 10)
else:
num2 = random.randint(1, 100)
if operator == '-' and num2 > num1:
num1, num2 = num2, num1
else: # grade 5
# Grade 5: Larger numbers and more complex operations
num1 = random.randint(1, 200)
operator = random.choice(['+', '-', '*', '/'])
if operator == '*':
num2 = random.randint(2, 15)
elif operator == '/':
num2 = random.randint(2, 15)
num1 = num2 * random.randint(1, 20)
else:
num2 = random.randint(1, 200)
if operator == '-' and num2 > num1:
num1, num2 = num2, num1
# Calculate answer
if operator == '+':
answer = num1 + num2
elif operator == '-':
answer = num1 - num2
elif operator == '*':
answer = num1 * num2
else: # operator == '/'
answer = num1 // num2
return Problem(num1, num2, operator, answer) |