From a82eae4159f6a72e078e003b6809009a3dac5f6c Mon Sep 17 00:00:00 2001 From: Heiko J Schick Date: Thu, 23 Mar 2023 20:51:01 +0100 Subject: [PATCH] Added documentation comments --- brain-jogging.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/brain-jogging.py b/brain-jogging.py index 816ddcd..ce92826 100644 --- a/brain-jogging.py +++ b/brain-jogging.py @@ -7,10 +7,33 @@ 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 + """ + Formats and returns a math problem string along with its result. + + Args: + a (int): The first operand in the math problem. + b (int): The second operand in the math problem. + operation (str): The mathematical operation ('+', '-', '*', '/') to be performed. + result (int): The result of the math problem. + + Returns: + tuple: A tuple containing the formatted math problem string and its 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(): + """ + Generates a random math problem with an operation chosen from addition, subtraction, multiplication, or division. + + The operands are randomly generated integers, with the following constraints: + - Both operands are within the range [0, 20] for addition and subtraction. + - Both operands are within the range [0, 20] for multiplication. + - For division, the divisor is within the range [1, 5] and the dividend is a multiple of the divisor, within the range [1, 40]. + + Returns: + tuple: A tuple containing the formatted math problem string and the correct result (integer). + """ operation = random.choice(['+', '-', '*', '/']) a, b = random.randint(0, 20), random.randint(0, 20)