Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

This is my code and my output on the console seems to be right but my tests won’t go through and they are hard to read so I don’t know exactly where to fix my code

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    first = ''
    second = ''
    lines = ''
    sumx = ''

    if len(problems) > 5:
        return('Error: Too many problems')

    for problem in problems:
        num1 = problem.split(' ')[0]
        num2 = problem.split(' ')[2]
        operator = problem.split(' ')[1]

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

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

        if(len(num1) >= 5 or (len(num2) >= 5)):
            return "Error: Numbers cannot be more than four digits."

        sum = ''
        if operator == '+':
            sum = str(int(num1)) + str(int(num2))
        elif(operator == "-"):
            sum = (int(num1)) - (int(num2))

        length = max(len(num1), len(num2)) + 2
        top = str(num1).rjust(length)
        bottom = operator + str(num2).rjust(length - 1)
        line = ""
        res = str(sum).rjust(length)
        for s in range (length):
            line += "-"

        if problem != problems[-1]:
            first += top + '   '
            second += bottom + '   '
            lines += line + '   '
            sumx += res + '   '
        else:
            first += top
            second += bottom
            lines += line
            sumx += res

    
        string = first + '\n' + second + '\n' + lines + '\n' + sumx
    else:
        string = first + '\n' + second + '\n' + lines
    return string





    return problems

print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 - 49"])}')

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Which test are you failing, and can you show example output please?

Did you check the errors in the browser dev console (F12) ?

The assertion error and diff gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

This is called a diff, and it shows you the differences between two files or blocks of code:

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!