Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:

Hello, I have written my code for the arithmetic formatter. However, I am encountering Assertion Errors. I am not sure what I am looking at. If anyone is able to point me in the right direction I would greatly appreciate it. Thanks.

Your code so far

Assertion error example:

arguments = [['3 + 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = '    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
E       assert '       3     3801     45     123\n   + 855   -    2   + 43   +  49\n   -----   ------   ----   -----' == '    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----'
E         -     3      3801      45      123
E         ?      -              -  -
E         +        3     3801     45     123
E         ? +++
E         - + 855    -    2    + 43    +  49
E         ?         -         -       -
E         +    + 855   -    2   + 43   +  49
E         ? +++
E         - -----    ------    ----    -----
E         ?      -            -    -
E         +    -----   ------   ----   -----
E         ? +++

test_module.py:77: AssertionError

I have attached a link to my replit url:
https://replit.com/@pnguyeddk/boilerplate-arithmetic-formatter#arithmetic_arranger.py

### Your browser information:

User Agent is: <code>Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0</code>

### Challenge Information:
Scientific Computing with Python Projects - Arithmetic Formatter
https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter

An assertion error 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

- 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!

Thank you for providing a reply. I want to ask you another question if you don’t mind. In my case, I have “-” on the “?” line, does that mean I have an extra space or something? And in another one of these “?” lines, I have 3 “+” signs, does that mean I am supposed to have another space?

I find this the easiest to interpret. Format it so you can compare the two lines clearly:

'       3     3801     45     123\n   + 855   -    2   + 43   +  49\n   -----   ------   ----   -----'
'    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----'

Your line is first. You can see you start off with too many spaces.
Again there is too many spaces between 3 and 3801.

Your line:

E         -     3      3801      45      123

Correct line:

E         +        3     3801     45     123

This line indicates the Difference between your line and the correct one:

? +++

Ah, its a bit confusing here now it looks like the opposite like the correct line is first with the “-” and the incorrect line starts with “+” and has 3 more spaces.

Not sure! I find the 2 line comparison I showed first to be easy to understand. I’m unsure what the dashes mean here as well:

?      -              -  -

That this character needs to be removed from the actual string?

There is some info here:
https://docs.pytest.org/en/7.1.x/how-to/output.html

I’ll update my explanation when I find a better explanation. This output looks different than the example I used in my explanation as well.

I’ve also found it useful to print the final output using repr() while troubleshooting:

print(repr(arranged_problems))

Which will print the raw string output as it appears above. It makes it easier to read spaces and newlines.

Where would you put the line :
print(repr(arranged_problems))
In the arithmetic_formatter.py program or in main.py?
Thank you.

in arithmetic_formatter.py just before you return the final output

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