Arithmetic formatter

Hi! I am having trouble formatting the results, I don’t undestand what the error is saying.
This is my code:
image

And this is the error:

Please post your actual code and error message instead of pictures. Thanks

def arithmetic_arranger(problems, results = False):
  arranged_problems = []
  solved_problems = []
  
  if len(problems) > 5:
    return "Error: Too many problems."
  for problem in problems:
    num1, op, num2 = problem.split()
    if op != "+" and op != "-":
      return "Error: Operator must be '+' or '-'."
    if not num1.isdigit() or not num2.isdigit():
      return "Error: Numbers must only contain digits."
    if len(num1) > 4 or len(num2) > 4:
      return "Error: Numbers cannot be more than four digits."
  
    ans = eval(problem)
    l = max(len(num1),len(num2)) + 2
    line1 = str(num1).rjust(l)
    line2 = op + "" + str(num2).rjust(l-1)
    dashes = ""
    for i in range(l):
      dashes = dashes + "-"
    line3 = str(ans).rjust(l)

    if results:
      solved_problems = f"{line1}\n{line2}\n{dashes}\n{line3}"
      arranged_problems = arranged_problems.append(solved_problems)
    else:
       solved_problems = f"{line1}\n{line2}\n{dashes}"
       arranged_problems = arranged_problems.append(solved_problems)
      
    return arranged_problems
None
============================= test session starts ==============================
platform linux -- Python 3.10.8, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /home/runner/boilerplate-arithmetic-formatter/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter
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 None == '  3801      123\n-    2    +  49\n------    -----'
E         +None
E         -'  3801      123\n-    2    +  49\n------    -----'

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 None == '  1         1\n+ 2    - 9380\n---    ------'
E         +None
E         -'  1         1\n+ 2    - 9380\n---    ------'

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

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 None == ('  11      3801      1      123         1\n'\n '+  4    - 2999    + 2    +  49    - 9380\n'\n '----    ------    ---    -----    ------')
E         +None
E         -'  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------'

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 None == '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
E         +None
E         -'    3      988\n+ 855    +  40\n-----    -----\n  858     1028'

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 None == ('   32         1      45      123      988\n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028')
E         +None
E         -'   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028'

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

The error messages say you are returning None, so you need to start looking there. I’d be suspicious of this line

What does list.append() return?

Hi again :worried:
I am still stuck on this. I have added new lines, removed spaces, removed new lines, etc and I still don’t get it to show as expected. I am not seeing any “?” in my error message to see what the difference is. Thanks.

def arithmetic_arranger(problems, results=False):
    for problem in problems:

        num1, op, num2 = problem.split()
        if len(problems) > 5:
            return "Error: Too many problems."
        if op != "+" and op != "-":
            return "Error: Operator must be '+' or '-'."
        if not num1.isdigit() or not num2.isdigit():
            return "Error: Numbers must only contain digits."
        if len(num1) > 4 or len(num2) > 4:
            return "Error: Numbers cannot be more than four digits."

        ans = eval(problem)
        l = max(len(num1), len(num2)) + 2
        line1 = num1.rjust(l)
        line2 = op + num2.rjust(l - 1)
        dashes = ""
        for i in range(l):
            dashes = dashes + "-"
        line3 = str(ans).rjust(l)

        if results:
            arranged_problems = f"{line1}\n{line2}\n{dashes}\n{line3}"
        else:
            arranged_problems = f"{line1}\n{line2}\n{dashes}"
          
        return arranged_problems

Error message:

   32
+ 698
-----
============ test session starts =============
platform linux -- Python 3.10.8, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /home/runner/boilerplate-arithmetic-formatter/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter
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 '  3801\n-    2\n------' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         - -    2    +  49
E         - ------    -----
E         +   3801
E         + -    2
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         - + 2    - 9380
E         - ---    ------
E         +   1
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 '  11\n+  4\n----' == ('  11      3801      1      123         1\n'\n '+  4    - 2999    + 2    +  49    - 9380\n'\n '----    ------    ---    -----    ------')
E         -   11      3801      1      123         1
E         - +  4    - 2999    + 2    +  49    - 9380
E         - ----    ------    ---    -----    ------
E         +   11
E         + +  4
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         - + 855    +  40
E         - -----    -----
E         -   858     1028
E         +     3
E         + + 855
E         + -----
E         +   858

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 '   32\n- 698\n-----\n -666' == ('   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         - - 698    - 3801    + 43    +  49    +  40
E         - -----    ------    ----    -----    -----
E         -  -666     -3800      88      172     1028
E         +    32
E         + - 698
E         + -----
E         +  -666

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.30s =========```
-   3801      123
- -    2    +  49
- ------    -----
+   3801
+ -    2
+ ------

This is what the first error message is telling you. You are only returning one problem now.

Hi! I was able to display all the problems and they look OK but I keep getting an error, something about a space I think. Could you please explain it to me?

def arithmetic_arranger(problems, results=True):
    line1 = ""
    line2 = ""
    line3 = ""
    dashes = ""
    for problem in problems:
        num1, op, num2 = problem.split()
        if len(problems) > 5:
            return "Error: Too many problems."
        if op != "+" and op != "-":
            return "Error: Operator must be '+' or '-'."
        if not num1.isdigit() or not num2.isdigit():
            return "Error: Numbers must only contain digits."
        if len(num1) > 4 or len(num2) > 4:
            return "Error: Numbers cannot be more than four digits."

        line1 = line1 + " "
        line2 = line2 + " "
        line3 = line3 + " "

        ans = eval(problem)
        l = max(len(num1), len(num2)) + 2
        line1 += num1.rjust(l)
        line2 += op + num2.rjust(l-1)
        dashes += " "
        for i in range(l):
            dashes = dashes + "-"
        line3 += str(ans).rjust(l)
      
        if results:
            arranged_problems = line1 + "\n" + line2 + "\n" + dashes + "\n" + line3
        else:
            arranged_problems = line1 + "\n" + line2 + "\n" + dashes

    return arranged_problems

    32   3801   45   123
 + 698 -    2 + 43 +  49
 ----- ------ ---- -----
   730   3799   88   172
================ test session starts =================
platform linux -- Python 3.10.8, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /home/runner/boilerplate-arithmetic-formatter/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter
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 '   3801   123\n -    2 +  49\n ------ -----\n   3799   172' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         ?          ---
E         +    3801   123
E         ? +
E         - -    2    +  49
E         ?        ---
E         +  -    2 +  49
E         ? +
E         - ------    -----
E         ?        ---
E         +  ------ -----
E         ? +            +
E         +    3799   172

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      1\n + 2 - 9380\n --- ------\n   3  -9379' == '  1         1\n+ 2    - 9380\n---    ------'
E         -   1         1
E         ?    ---
E         +    1      1
E         ? +
E         - + 2    - 9380
E         ?     ---
E         +  + 2 - 9380
E         ? +
E         - ---    ------
E         ?    ---
E         +  --- ------
E         ? +          +
E         +    3  -9379

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

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 ('   11   3801   1   123      1\n'\n ' +  4 - 2999 + 2 +  49 - 9380\n'\n ' ---- ------ --- ----- ------\n'\n '   15    802   3   172  -9379') == ('  11      3801      1      123         1\n'\n '+  4    - 2999    + 2    +  49    - 9380\n'\n '----    ------    ---    -----    ------')
E         -   11      3801      1      123         1
E         ?     ---          --- ---            ---
E         +    11   3801   1   123      1
E         ? +
E         - +  4    - 2999    + 2    +  49    - 9380
E         ?      ---       ---    ---      ---
E         +  +  4 - 2999 + 2 +  49 - 9380
E         ? +
E         - ----    ------    ---    -----    ------
E         ?     ---        ---   ---      ---
E         +  ---- ------ --- ----- ------
E         ? +                            +
E         +    15    802   3   172  -9379

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   988\n + 855 +  40\n ----- -----\n   858  1028' == '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
E         -     3      988
E         ?      ---
E         +      3   988
E         ? +
E         - + 855    +  40
E         ?       ---
E         +  + 855 +  40
E         ? +
E         - -----    -----
E         ?      ---
E         +  ----- -----
E         ? +
E         -   858     1028
E         ?      ---
E         +    858  1028
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 ('    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         ?            --- ---     ---      ---
E         +     32      1   45   123   988
E         ? +
E         - - 698    - 3801    + 43    +  49    +  40
E         ?       ---       ---     ---      ---
E         +  - 698 - 3801 + 43 +  49 +  40
E         ? +
E         - -----    ------    ----    -----    -----
E         ?      ---        ---    ---      ---
E         +  ----- ------ ---- ----- -----
E         ? +
E         -  -666     -3800      88      172     1028
E         ?      ---          ---  ---      ---
E         +   -666  -3800   88   172  1028
E         ? +

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.34s =============
 

Hi! I don’t understand what’s wrong with my code now. It looks the same to me.
image

Code and error below, thanks for the help.

def arithmetic_arranger(problems, results=True):
    line1 = ""
    line2 = ""
    line3 = ""
    dashes = ""
    for problem in problems:
        num1, op, num2 = problem.split()
        if len(problems) > 5:
            return "Error: Too many problems."
        if op != "+" and op != "-":
            return "Error: Operator must be '+' or '-'."
        if not num1.isdigit() or not num2.isdigit():
            return "Error: Numbers must only contain digits."
        if len(num1) > 4 or len(num2) > 4:
            return "Error: Numbers cannot be more than four digits."

        line1 = line1 + ""
        line2 = line2 + ""
        line3 = line3 + ""

        ans = eval(problem)
        l = max(len(num1), len(num2)) + 2
        line1 += num1.rjust(l) + "   "
        line2 += op + num2.rjust(l - 1) + "   "
        dashes += ""
        for i in range(l):
            dashes = dashes + "-"
        line3 += str(ans).rjust(l) + "   "
        for d in range(3):
            dashes = dashes + " "
        
        if results:
            arranged_problems = line1 + "\n" + line2 + "\n" + dashes + "\n" + line3
        else:
            arranged_problems = line1 + "\n" + line2 + "\n" + dashes

    return arranged_problems
 32     3801     45     123   
+ 698   -    2   + 43   +  49   
-----   ------   ----   -----   
======================== test session starts ========================
platform linux -- Python 3.10.8, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /home/runner/boilerplate-arithmetic-formatter/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter
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 '  3801     123   \n-    2   +  49   \n------   -----   ' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         ?            -
E         +   3801     123   
E         ?               +++
E         - -    2    +  49
E         ?          -
E         + -    2   +  49   
E         ?               +++
E         - ------    -----
E         ?          -
E         + ------   -----   
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        1   \n+ 2   - 9380   \n---   ------   ' == '  1         1\n+ 2    - 9380\n---    ------'
E         -   1         1
E         ?            -
E         +   1        1   
E         ?             +++
E         - + 2    - 9380
E         ?       -
E         + + 2   - 9380   
E         ?             +++
E         - ---    ------
E         ?    -
E         + ---   ------   
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     3801     45     123   \n'\n '+ 855   -    2   + 43   +  49   \n'\n '-----   ------   ----   -----   ') == ('    3      3801      45      123\n'\n '+ 855    -    2    + 43    +  49\n'\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
___________ 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 ('  11     3801     1     123        1   \n'\n '+  4   - 2999   + 2   +  49   - 9380   \n'\n '----   ------   ---   -----   ------   ') == ('  11      3801      1      123         1\n'\n '+  4    - 2999    + 2    +  49    - 9380\n'\n '----    ------    ---    -----    ------')
E         -   11      3801      1      123         1
E         ?     -              - -                -
E         +   11     3801     1     123        1   
E         ?                                     +++
E         - +  4    - 2999    + 2    +  49    - 9380
E         ?        -         -      -        -
E         + +  4   - 2999   + 2   +  49   - 9380   
E         ?                                     +++
E         - ----    ------    ---    -----    ------
E         ?     -            -   -        -
E         + ----   ------   ---   -----   ------   
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     988   \n+ 855   +  40   \n-----   -----   \n  858    1028   ' == '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
E         -     3      988
E         ?      -
E         +     3     988   
E         ?              +++
E         - + 855    +  40
E         ?         -
E         + + 855   +  40   
E         ?              +++
E         - -----    -----
E         ?         -
E         + -----   -----   
E         ?              +++
E         -   858     1028
E         ?      -
E         +   858    1028   
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 ('   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         ?              - -       -             -
E         +    32        1     45     123     988   
E         ?                                      +++
E         - - 698    - 3801    + 43    +  49    +  40
E         ?         -         -       -        -
E         + - 698   - 3801   + 43   +  49   +  40   
E         ?                                      +++
E         - -----    ------    ----    -----    -----
E         ?      -            -    -           -
E         + -----   ------   ----   -----   -----   
E         ?                                      +++
E         -  -666     -3800      88      172     1028
E         ?      -              -  -        -
E         +  -666    -3800     88     172    1028   
E         ?                                      +++

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.22s ====================
 

The tests are telling you that you have extra spaces at the end of lines

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