FreeCodeCamp project arithmetic arranger python

I have been learning python for a few weeks now and after completing the freecodecamp videos about it i started doing the project. In this project I have to make an arithmetic arranger. I wrote the code that prints it out and i went over every rule they gave us, but my code still passes only 4 tests and fails 6. I know we should do the projects on our own but, I just cant find whats wrong since i get the right output. Could you give me a hint or some help on how I can fix it?
Here is the to my code: link https://replit.com/@TheMystic3/boilerplate-arithmetic-formatter-11#arithmetic_arranger.py
And here is the link to the project: https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter

1 Like
def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return "Error: Too many problems"
    formatted_problems = []

    for problem in problems:
        parts = problem.split()
        if len(parts) != 3 or parts[1] not in "+-":
            return (
                "Error: Each problem must contain two operands and one operator with"
                " '+' or '-'."
            )
        operand1, operator, operand2 = parts
        if not operand1.isdigit() or not operand2.isdigit():
            return "Error: Numbers must only contain digits."
        if len(operand1) > 4 or len(operand2) > 4:
            return "Error: Numbers cannot be more than four digits."
        max_length = max(len(operand1), len(operand2))
        top_line = operand1.rjust(max_length + 2)
        bottom_line = operator + operand2.rjust(max_length + 1)
        dash_line = "-" * (max_length + 2)

        formatted_problem = [top_line, bottom_line, dash_line]

        if show_answers:
            answer = str(eval(problem)).rjust(max_length + 2)
            formatted_problem.append(answer)
        formatted_problems.append(formatted_problem)
    arranged_problems = ["    ".join(parts) for parts in zip(*formatted_problems)]

    return "\n".join(arranged_problems)


problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
problems_with_answers = ["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"]

print(arithmetic_arranger(problems))
print(arithmetic_arranger(problems_with_answers, True))
1 Like

thanks, your code has fixed most of the fails i get but i still get 2 more which should be working.
I have fixed 1 one of them but the second 1 i cant figure out. Do u mind taking 1 more look?
Here is the link: https://replit.com/@TheMystic3/boilerplate-arithmetic-formatter-10#arithmetic_arranger.py