Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Hello! Could you please help me?
The details of the problem:
When I run the tests, the tests whose outputs are supposed to be Error messages pass the test. But, the remaining tests, where correct values are inputted, they fail the test. I have spent a lot of time looking for errors in my code, and as well as manually checking the expected test output, and my test output; and run the tests again, but they don’t pass the test. Hence, could you please check ad let me know what is wrong with it?

My code so far:

def arithmetic_arranger(problems, show_answers=False):
    if len(problems)>5:
        return 'Error: Too many problems.'

    operands1=''
    line2=''
    lines=''
    answers=''

    for problem in problems:

        operand1=problem.split(' ')[0]
        operator = problem.split(' ')[1]
        operand2= problem.split(' ')[2]

        if operator != '+' and operator != '-':
            return "Error: Operator must be '+' or '-'."

        if not(operand1.isdigit() and 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.'

        

        
        line=''
        if operator=='+':
            answer = str(int(operand1) + int(operand2))
        elif operator=='-':
            answer = str(int(operand1) - int(operand2))

        
        width = max(len(operand1), len(operand2))+2


        if problem!=problems[-1]:
            operands1 += str(operand1).rjust(width)+'      '
            line2 += operator+str(operand2).rjust(width-1)+'      '
            lines += '-'*width +'      '
            answers += str(answer).rjust(width)+'      '

        else:
            operands1 += str(operand1).rjust(width)
            line2 += operator+str(operand2).rjust(width-1)
            lines += '-'*width
            answers += str(answer).rjust(width)




    if show_answers==True:
        arranged=operands1 + '\n' + line2 + '\n' + lines + '\n' + answers

    elif show_answers==False:
        arranged=operands1 + '\n' + line2 + '\n' + lines
    
    return arranged

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

    


Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

There’s a note at the end of the instructions but it’s easy to miss:

Note: open the browser console with F12 to see a more verbose output of the tests.

Also helps to use repr() to print your function so you can compare the output with the tests format:

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

I opened the Developer Tools, but I have just started, and thus couldn’t make out anything from it. There were a lot of issues it showed, like 99+ it showed

And as for using repr(), the output was all jumbled up, none of it made sense

Show me.

It should match the test output:
arithmetic_arranger(["3801 - 2", "123 + 49"]) should return 3801 123\n- 2 + 49\n------ -----

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

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

This shows the string with the formatting characters like space and \n clearly so you can compare.

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

You can see that you have too many spaces between 3801 and 123.

As for the console output it can be a mess to parse at first, but look for the assertion error and diff output.

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!

So like for this, in the tests, each test expect different no. of spaces, so could you please help me with that in my code?

For example:
In the test,

arithmetic_arranger(["3801 - 2", "123 + 49"])` should return 
`  3801      123\n-    2    +  49\n------    -----

There are 6 spaces,

But in another test,

arithmetic_arranger(["1 + 2", "1 - 9380"])` should return 
`  1         1\n+ 2    - 9380\n---    ------

There are 9 spaces… so could you please help me with this?

This exercise suggests opening the browser console to view the detailed output from the tests. Have you tried this?

Edit: reading your older post I see that you did try but you don’t know how to interpret the result. But you’ve already received some comments above that should help you with that as well.

This should be apparent to you since your function works in a similar way. There are more spaces between 1s because 1 is only 1 character and 123 is 3 characters so it maintains a fixed distance between equations.

Your code also maintains a fixed distance between equations:

   32           1        45        123        988
- 698      - 3801      + 43      +  49      +  40
-----      ------      ----      -----      -----

You have 6 spaces between each set of ---

Look at the example in the instructions:

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

Only 4 spaces between each set of ---.

Compare your output carefully with the output the test requires: