# Arithmetic Formatter Help: Assertion Error?

Can anybody help? Trying to find where i went wrong.

``````def arithmetic_arranger(problems, start = False):

arranged_problems = ""
values_top = ""
values_bot = ""
bot_lines = ""
results = ""

for problem in problems:

#parsing into elements
elements = problem.split(" ")
first = elements[0]
second = elements[2]
operator = elements[1]

#error conditions
if len(problems) > 5:
return "Error: too many problems."
elif len(first) > 4 or len(second) > 4:
return "Error: Numbers cannot be more than four digits."
elif not first.isnumeric() or not second.isnumeric():
return "Error: Numbers must only contain digits."
elif operator != '+' or operator != '-':
return "Error: Operator must be '+' or '-'."

#math thing
if operator == '+':
result = str(int(first)+int(second))
elif operator == '-':
result = str(int(first)-int(second))
#arranging
maxlength = max(len(first), len(second)) + 2
topelement = first.rjust(maxlength)
botelement = operator + second.rjust(maxlength - 1)
result = result.rjust(maxlength)
dashmarks = str()
for wspaces in range(maxlength):
dashmarks += "-"

if problem != problems[-1]:
values_top += topelement + '     '
values_bot += botelement + '     '
bot_lines += dashmarks + '     '
results += result + '     '
else:
values_top += topelement
values_bot += botelement
bot_lines += dashmarks
results + result
if start:
arranged_problems = values_top+"\n"+values_bot+"\n"+bot_lines+"\n"+results
else:
arranged_problems = values_top+"\n"+values_bot+"\n"+bot_lines
return arranged_problems
``````

Output:

``````îş§ python main.py
Error: Operator must be '+' or '-'.
============================= test session starts ==============================
platform linux -- Python 3.8.13, pytest-7.1.1, pluggy-1.0.0 -- /nix/store/06c55y0c5yzx5gx4l6k0pp6071zf1y5i-python3-3.8.13/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] FAILED             [ 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: Operator must be '+' or '-'." == '  3801      123\n-    2    +  49\n------    -----'
E         + Error: Operator must be '+' or '-'.
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 "Error: Operator must be '+' or '-'." == '  1         1\n+ 2    - 9380\n---    ------'
E         + Error: Operator must be '+' or '-'.
E         -   1         1
E         - + 2    - 9380
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 "Error: Operator must be '+' or '-'." == ('    3      3801      45      123\n'\n '+ 855    -    2    + 43    +  49\n'\n '-----    ------    ----    -----')
E         + Error: Operator must be '+' or '-'.
E         -     3      3801      45      123
E         - + 855    -    2    + 43    +  49
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: Operator must be '+' or '-'." == ('  11      3801      1      123         1\n'\n '+  4    - 2999    + 2    +  49    - 9380\n'\n '----    ------    ---    -----    ------')
E         + Error: Operator must be '+' or '-'.
E         -   11      3801      1      123         1
E         - +  4    - 2999    + 2    +  49    - 9380
E         - ----    ------    ---    -----    ------

test_module.py:77: AssertionError
____________________ test_template[test_too_many_problems] _____________________

arguments = [['44 + 815', '909 - 2', '45 + 43', '123 + 49', '888 + 40', '653 + 87']]
expected_output = 'Error: Too many problems.'
fail_message = 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."'

@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 calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."
E       assert 'Error: too many problems.' == 'Error: Too many problems.'
E         - Error: Too many problems.
E         ?        ^
E         + Error: too many problems.
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 "Error: Operator must be '+' or '-'." == '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
E         + Error: Operator must be '+' or '-'.
E         -     3      988
E         - + 855    +  40
E         - -----    -----
E         -   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 "Error: Operator must be '+' or '-'." == ('   32         1      45      123      988\n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028')
E         + Error: Operator must be '+' or '-'.
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] - 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_too_many_problems] - AssertionError...
FAILED test_module.py::test_template[test_two_problems_with_solutions] - Asse...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - Ass...
========================= 7 failed, 3 passed in 0.27s ======================
``````

Consider the part catching errors:

``````    if len(problems) > 5:
return "Error: too many problems."
elif len(first) > 4 or len(second) > 4:
return "Error: Numbers cannot be more than four digits."
elif not first.isnumeric() or not second.isnumeric():
return "Error: Numbers must only contain digits."
elif operator != '+' or operator != '-':
return "Error: Operator must be '+' or '-'."
``````

Try going through it step-by-step with some examples. What happens when used is incorrect operator, what happens when used is correct operator?

1 Like

you are returning the error instead of the arranged numbers.

that happens here in your code
try to figure out why

1 Like

I got rid of the line. Instead, wrote it bottom of the if statement that doing math operations:

``````    #math thing
if operator == '+':
result = str(int(first)+int(second))
elif operator == '-':
result = str(int(first)-int(second))
else:
return "Error: Operator must be '+' or '-'."
``````

Thank you for your help. I have got 2 more fails to get rid of thoughâ€¦

I corrected all of my fails. I donâ€™t know why the code gave me this â€śoperatorâ€ť error, but i fixed it like this:

``````def arithmetic_arranger(problems, start = False):

arranged_problems = ""
values_top = ""
values_bot = ""
bot_lines = ""
results = ""

for problem in problems:

#parsing into elements
elements = problem.split(" ")
first = elements[0]
operator = elements[1]
second = elements[2]

#error conditions
if len(problems) >= 6:
return "Error: Too many problems."

if len(first) > 4 or len(second) > 4:
return "Error: Numbers cannot be more than four digits."

if not first.isnumeric() or not second.isnumeric():
return "Error: Numbers must only contain digits."
#math thing
if operator == '+':
result = str(int(first)+int(second))
elif operator == '-':
result = str(int(first)-int(second))
else:
return "Error: Operator must be '+' or '-'."
#arranging
maxlength = max(len(first), len(second)) +2
topelement = first.rjust(maxlength)
botelement = operator + second.rjust(maxlength - 1)
result = result.rjust(maxlength)
dashmarks = str()
for wspaces in range(maxlength):
dashmarks += "-"

if problem != problems[-1]:
values_top += topelement + '    '
values_bot += botelement + '    '
bot_lines += dashmarks + '    '
results += result + '    '
else:
values_top += topelement
values_bot += botelement
bot_lines += dashmarks
results += result
if start:
arranged_problems = values_top+"\n"+values_bot+"\n"+bot_lines+"\n"+results
else:
arranged_problems = values_top+"\n"+values_bot+"\n"+bot_lines
return arranged_problems
``````

Thanks again. I hope i can improve myself more.

You are using `or`, that means that the whole expression evaluates to true if one of the two parts evaluates to true.

if `operator` is equal to `+` you have that `operator != '+'` is `False` and `operator != '-'` is `True`, and `False or True` is `True`, so you give back the error.
To fix the error you need to change the logic operator here.

1 Like

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