Minor reorganisation
This commit is contained in:
+13
-15
@@ -6,27 +6,25 @@ from reportlab.lib.styles import getSampleStyleSheet
|
||||
from reportlab.pdfbase import pdfmetrics
|
||||
from reportlab.pdfbase.ttfonts import TTFont
|
||||
|
||||
def format_and_return(a, b, operation, result):
|
||||
a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ')
|
||||
return f"{a_str} {operation} {b_str} =", result
|
||||
|
||||
def generate_math_problem():
|
||||
operation = random.choice(['+', '-', '*', '/'])
|
||||
a, b = random.randint(0, 20), random.randint(0, 20)
|
||||
|
||||
if operation == '+':
|
||||
a, b = random.randint(0, 20), random.randint(0, 20)
|
||||
a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ')
|
||||
return f"{a_str} {operation} {b_str} =", a + b
|
||||
return format_and_return(a, b, operation, a + b)
|
||||
elif operation == '-':
|
||||
a, b = random.randint(0, 20), random.randint(0, 20)
|
||||
if a < b:
|
||||
a, b = b, a
|
||||
a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ')
|
||||
return f"{a_str} {operation} {b_str} =", a - b
|
||||
a, b = max(a, b), min(a, b)
|
||||
return format_and_return(a, b, operation, a - b)
|
||||
elif operation == '*':
|
||||
a, b = random.randint(0, 20), random.randint(0, 20)
|
||||
a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ')
|
||||
return f"{a_str} {operation} {b_str} =", a * b
|
||||
return format_and_return(a, b, operation, a * b)
|
||||
elif operation == '/':
|
||||
b = random.randint(1, 10) # Divisor
|
||||
a = b * random.randint(1, 20 // b) # Dividend (as a multiple of the divisor)
|
||||
a_str, b_str = str(a).rjust(2, ' '), str(b).rjust(2, ' ')
|
||||
return f"{a_str} {operation} {b_str} =", a // b
|
||||
b = random.randint(1, 5)
|
||||
a = b * random.randint(1, 40 // b)
|
||||
return format_and_return(a, b, operation, a // b)
|
||||
|
||||
def create_brain_jogging_pdf(filename):
|
||||
problems = [[generate_math_problem() for _ in range(4)] for _ in range(20)]
|
||||
|
||||
Reference in New Issue
Block a user