Minor reorganisation

This commit is contained in:
Heiko J Schick
2023-03-23 20:45:11 +01:00
parent 91169befad
commit 1b6611d8c4
+13 -15
View File
@@ -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)]