From 1b6611d8c4328b89c20254eb409afd428f85f8a1 Mon Sep 17 00:00:00 2001 From: Heiko J Schick Date: Thu, 23 Mar 2023 20:45:11 +0100 Subject: [PATCH] Minor reorganisation --- brain-jogging.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/brain-jogging.py b/brain-jogging.py index 758644f..816ddcd 100644 --- a/brain-jogging.py +++ b/brain-jogging.py @@ -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)]