Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My output prints out correctly . but when I run the tests the test marks it wrong . what could the problem be ?

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    # Check if there are too many problems (limit is 5)
    if len(problems) > 5:
        return "Error: Too many problems."

    first = ""
    second = ""
    lines = ""
    solt = ""

    for problem in problems:
        parts = problem.split()
        # Split problem into three parts, separating operands from operator
        first_problem, operator, second_problem = parts

        if '+' not in operator and '-' not in operator:
            return "Error: Operator must be '+' or '-'."

        if not first_problem.isdigit() or not second_problem.isdigit():
            return "Error: Numbers must only contain digits."

        if len(first_problem) >= 5 or len(second_problem) >= 5:
            return 'Error: Numbers cannot be more than four digits.'

        length = max(len(first_problem), len(second_problem)) + 2
        top = first_problem.rjust(length)
        bottom = operator + second_problem.rjust(length - 1)
        line = '-' * length

        # Perform the operation based on the operator
        if operator == '+':
            res = str(int(first_problem) + int(second_problem)).rjust(length)
        elif operator == '-':
            res = str(int(first_problem) - int(second_problem)).rjust(length)

        first += top + "    "
        second += bottom + "    "
        lines += line + "    "
        solt += res + "    "

    if show_answers:
        arithmetic_arranger = first + '\n' + second + '\n' + lines + '\n' + solt
    else:
        arithmetic_arranger = first + '\n' + second + '\n' + lines

    return arithmetic_arranger

# Example usage:
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"], True)}')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.46

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Are you sure its identical to the target output down to every last space? You seem to have extra space at the end of every line.

I dont seem to find the extra space you pointing out to . is it the spacing between each problem?

Right here. You add this to the right of every single problem. But the tests don’t want extra whitespace at the end of the line.

it says in the instructions * There should be four spaces between each problem.
so I used the whitespaces to seperate the output of each problem

Between, yes.

Good:

problem1 4spaces problem2

Bad:

problem1 4spaces problem2 4spaces

Good:

problem1 4spaces problem2 4spaces problem3

Bad:

problem1 4spaces problem2 4spaces problem3 4spaces

ok let me make few changes and see how it comes out.

1 Like

You can use the browser dev tools console (f12) for a more detailed error message.

Also, you can use repr() to format your output in the same format that the tests display it:

print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"], True)))

>>>'  3801      123    \n-    2    +  49    \n------    -----    \n  3799      172    '

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.