Arithmetic Formatter

Above is the link to my replit, I have 4 passes and 6 fails, and I don’t know why I’ve been stuck for days, and for some reason, my code won’t print out beyond the first problem in the problem set. I just need someone to point out a mistake in my code to help me get started

Which tests are failing? What’s the output of the test suite?

I would expect that the test output doesn’t like your dashes, at a guess?

I am failing everything except the error checks so the ones where we have to make it display an error message if something is wrong with the input I am passing the test failing. The issue is that the string isn’t outputting like at all, it will not show any of the calculated ones and the ones where they aren’t calculated they will only show the first math question in the list and won’t output the rest. Idk why at all

Please post the output from the tests

     32     
+  698    
------    
======================= test session starts =======================
platform linux -- Python 3.10.11, pytest-6.2.5, py-1.11.0, pluggy-1.4.0 -- /nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/bin/python3
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter-5
collected 10 items                                                

test_module.py::test_template[test_two_problems_arrangement1] FAILED [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] FAILED [ 20%]
test_module.py::test_template[test_four_problems_arrangement] FAILED [ 30%]
test_module.py::test_template[test_five_problems_arrangement] FAILED [ 40%]
test_module.py::test_template[test_too_many_problems] PASSED [ 50%]
test_module.py::test_template[test_incorrect_operator] PASSED [ 60%]
test_module.py::test_template[test_too_many_digits] PASSED  [ 70%]
test_module.py::test_template[test_only_digits] PASSED      [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] FAILED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] FAILED [100%]

============================ FAILURES =============================
__________ test_template[test_two_problems_arrangement1] __________

arguments = [['3801 - 2', '123 + 49']]
expected_output = '  3801      123\n-    2    +  49\n------    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "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 ["3801 - 2", "123 + 49"]
E       assert 'Error: Numbers cannot be more than four digits.' == '  3801      123\n-    2    +  49\n------    -----'
E         + Error: Numbers cannot be more than four digits.
E         -   3801      123
E         - -    2    +  49
E         - ------    -----

test_module.py:77: AssertionError
__________ test_template[test_two_problems_arrangement2] __________

arguments = [['1 + 2', '1 - 9380']]
expected_output = '  1         1\n+ 2    - 9380\n---    ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]'

    @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 ["1 + 2", "1 - 9380"]
E       assert '   1     \n+  2    \n----    ' == '  1         1\n+ 2    - 9380\n---    ------'
E         -   1         1
E         ?         -----
E         +    1     
E         ? +
E         - + 2    - 9380
E         - ---    ------
E         + +  2    
E         + ----

test_module.py:77: AssertionError
__________ test_template[test_four_problems_arrangement] __________

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     \n+  855    \n------    ' == ('    3      3801      45      123\n'\n '+ 855    -    2    + 43    +  49\n'\n '-----    ------    ----    -----')
E         -     3      3801      45      123
E         - + 855    -    2    + 43    +  49
E         - -----    ------    ----    -----
E         +      3     
E         + +  855    
E         + ------

test_module.py:77: AssertionError
__________ test_template[test_five_problems_arrangement] __________

arguments = [['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']]
expected_output = '  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]'

    @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 ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]
E       assert 'Error: Too many problems.' == ('  11      3801      1      123         1\n'\n '+  4    - 2999    + 2    +  49    - 9380\n'\n '----    ------    ---    -----    ------')
E         + Error: Too many problems.
E         -   11      3801      1      123         1
E         - +  4    - 2999    + 2    +  49    - 9380
E         - ----    ------    ---    -----    ------

test_module.py:77: AssertionError
_________ test_template[test_two_problems_with_solutions] _________

arguments = [['3 + 855', '988 + 40'], True]
expected_output = '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.'

    @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 solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.
E       assert '     3     \n+  855    \n------    \n   858    ' == '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
E         -     3      988
E         ?           ----
E         +      3     
E         ? +
E         - + 855    +  40
E         ?          -----
E         + +  855    
E         ?  +
E         - -----    -----
E         + ------    
E         -   858     1028
E         ?          -----
E         +    858    
E         ? +

test_module.py:77: AssertionError
________ test_template[test_five_problems_with_solutions] _________

arguments = [['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True]
expected_output = '   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.'

    @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 solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E       assert 'Error: Too many problems.' == ('   32         1      45      123      988\n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028')
E         + Error: Too many problems.
E         -    32         1      45      123      988
E         - - 698    - 3801    + 43    +  49    +  40
E         - -----    ------    ----    -----    -----
E         -  -666     -3800      88      172     1028

test_module.py:77: AssertionError
===================== short test summary info =====================
FAILED test_module.py::test_template[test_two_problems_arrangement1]
FAILED test_module.py::test_template[test_two_problems_arrangement2]
FAILED test_module.py::test_template[test_four_problems_arrangement]
FAILED test_module.py::test_template[test_five_problems_arrangement]
FAILED test_module.py::test_template[test_two_problems_with_solutions]
FAILED test_module.py::test_template[test_five_problems_with_solutions]
=================== 6 failed, 4 passed in 0.61s ===================

it misaligned some of the outputs in the code i have it aligned corretly if they look if its due to it beign copied




I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

this one is outputting the error message instead of the formatted numbers, maybe you need to check well when your error messages happen?

for this one, your formatting is not accurate and you are missing the second problem

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 this helped a lot but I’m still stuck, I’ve managed to fix a lot of the formatting errors I had using your help except this one.

E       AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E       assert ('   32         1      45      123      988 \n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028') == ('   32         1      45      123      988\n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028')
E         -    32         1      45      123      988
E         +    32         1      45      123      988 
E         ?                                          +
E           - 698    - 3801    + 43    +  49    +  40
E           -----    ------    ----    -----    -----
E            -666     -3800      88      172     1028

Based on my knowledge and interpretation of your help the error is that there is an extra space on the top line where there doesn’t need to be but I’ve looked through my code and I can’t see where the extra space had been added at the end of the top line of the code.
below is what is printed from my code as an example:

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

based of this, I’m sure that is just an extra random space at the end of it that can’t be seen when printed out (because its just an empty space) but is causing the error but i can’t find it in my code at all.

Below is the link to my code, can you tell me if I’ve interpreted the error incorrectly and I should be looking elsewhere for an error or if I’m right and there isn’t an error in my code?

I have added

    top = str(operands[0]).rjust(length) 
    print(f'"{top}"')

and it gives

"   32 "
"     1 "
"  45 "
"  123 "
"  988 "

so I guess it comes from here

But that looks correct, the 1 is above a 4 digit number so it should be printed on top of the 4th digit and be shifted to the left of the three digits numbers the 32 is above a 3 digit so it should start printing on the 3rd digit and the 45 on top of a 2 digit so should print on the 2nd digit. Meaning that the 32 being shifted on the the right of the 45 make sense. so they all line up if that make sense. maybe they are all meant to be shifted to the right??? but still the error is just saying there is an empty gap next to the 988 if the spacing was wrong between them ether would be errors to show that. I’m just really confused and feel like i’m going in circles changing the same things in my code.

I am saying that the issue is the space to the right of each of those characters, as the last number in the row will have an extra space before the new line character.
If you think your logic is sound and there is no need to change it, then focus on adding code to remove the space before the new line character is added.

ohhh sorry that makes sense, i’ll see if i can remove the space

thank you that was the error

else:
      first_line +=  "".join(top.rstrip()) 

after i made that change it passed all the checks. thanks again

you can avoid the join there, only rstrip is necessary

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