# Scientific Computing with Python - Arithmetic Formatter project

hey i’m working on Scientific Computing with Python - and i’m stock on Arithmetic Formatter project i keep getting fail

def arithmetic_arranger(problems,option=None):
if len(problems) > 5:
return  " Error: Too many problems. "
else:
top = ''
bottom = ''
line = ''
res = ''
for x in problems :
problem = x.split()
first_num = problem[0]
operator = problem[1]
second_num = problem[2]
if ( len(first_num) > 4 or len(second_num) > 4 ) :
return " Error: Numbers cannot be more than four digits. "
elif not first_num.isnumeric() or not second_num.isnumeric():
return " Error: Numbers must only contain digits. "
elif operator != '+' and operator != '-':
return " Error: Operator must be '+' or '-'. "
else :
if operator == '+' :
sum = str(int(first_num) + int(second_num))
else :
sum = str(int(first_num) - int(second_num))
length = max(len(first_num),len(second_num)) + 2
topX = first_num.rjust(length)
bottomX = operator + second_num.rjust(length - 1)
lineX = ''
for i in range(length):
lineX = lineX + '-'
resX = sum.rjust(length)
if x != problems[-1]:
top += topX + '    '
bottom += bottomX + '    '
line += lineX + '    '
res += resX + '    '
else :
top += topX
bottom += bottomX
line += lineX
res += resX
top.rstrip()
bottom.rstrip()
line.rstrip()
res.rstrip()
if option == True:
return f'\n {top} \n {bottom} \n {line} \n {res}'
else:
return f'\n {top} \n {bottom} \n {line}'

Paste your errors here please.

1 Like
32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
============================= test session starts ==============================
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /home/runner/boilerplate-arithmetic-formatter-2/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter-2
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] FAILED            [ 60%]
test_module.py::test_template[test_too_many_digits] FAILED               [ 70%]
test_module.py::test_template[test_only_digits] FAILED                   [ 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 '\n   3801      123 \n -    2    +  49 \n ------    -----' == '  3801      123\n-    2    +  49\n------    -----'
E         +
E         -   3801      123
E         +    3801      123
E         ? +               +
E         - -    2    +  49
E         +  -    2    +  49
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 '\n   1         1 \n + 2    - 9380 \n ---    ------' == '  1         1\n+ 2    - 9380\n---    ------'
E         +
E         -   1         1
E         +    1         1
E         ? +             +
E         - + 2    - 9380
E         +  + 2    - 9380
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 ('\n'\n '     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         +
E         -     3      3801      45      123
E         +      3      3801      45      123
E         ? +                                +
E         - + 855    -    2    + 43    +  49
E         +  + 855    -    2    + 43    +  49
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 ('\n'\n '   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         +
E         -   11      3801      1      123         1
E         +    11      3801      1      123         1
E         ? +                                        +
E         - +  4    - 2999    + 2    +  49    - 9380
E         +  +  4    - 2999    + 2    +  49    - 9380
E         ? +                                        +
E         - ----    ------    ---    -----    ------
E         +  ----    ------    ---    -----    ------
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         +  Error: Too many problems.
E         ? +                         +

test_module.py:77: AssertionError
____________________ test_template[test_incorrect_operator] ____________________

arguments = [['3 / 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = "Error: Operator must be '+' or '-'."
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be \'+\' or \'-\'."'

@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 a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."
E       assert " Error: Operator must be '+' or '-'. " == "Error: Operator must be '+' or '-'."
E         - Error: Operator must be '+' or '-'.
E         +  Error: Operator must be '+' or '-'.
E         ? +                                   +

test_module.py:77: AssertionError
_____________________ test_template[test_too_many_digits] ______________________

arguments = [['24 + 85215', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = 'Error: Numbers cannot be more than four digits.'
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."'

@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 a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."
E       assert ' Error: Numbers cannot be more than four digits. ' == 'Error: Numbers cannot be more than four digits.'
E         - Error: Numbers cannot be more than four digits.
E         +  Error: Numbers cannot be more than four digits.
E         ? +                                               +

test_module.py:77: AssertionError
_______________________ test_template[test_only_digits] ________________________

arguments = [['98 + 3g5', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = 'Error: Numbers must only contain digits.'
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."'

@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 a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."
E       assert ' Error: Numbers must only contain digits. ' == 'Error: Numbers must only contain digits.'
E         - Error: Numbers must only contain digits.
E         +  Error: Numbers must only contain digits.
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 '\n     3      988 \n + 855    +  40 \n -----    ----- \n   858     1028' == '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
E         +
E         -     3      988
E         +      3      988
E         ? +              +
E         - + 855    +  40
E         +  + 855    +  40
E         ? +              +
E         - -----    -----
E         +  -----    -----
E         ? +              +
E         -   858     1028
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 ('\n'\n '    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         +
E         -    32         1      45      123      988
E         +     32         1      45      123      988
E         ? +                                         +
E         - - 698    - 3801    + 43    +  49    +  40
E         +  - 698    - 3801    + 43    +  49    +  40
E         ? +                                         +
E         - -----    ------    ----    -----    -----
E         +  -----    ------    ----    -----    -----
E         ? +                                         +
E         -  -666     -3800      88      172     1028
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] - 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_incorrect_operator] - AssertionErro...
FAILED test_module.py::test_template[test_too_many_digits] - AssertionError: ...
FAILED test_module.py::test_template[test_only_digits] - AssertionError: Expe...
FAILED test_module.py::test_template[test_two_problems_with_solutions] - Asse...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - Ass...
============================== 10 failed in 0.37s ==============================


Not sure if you noticed, but one of the errors is the indentation of the numbers.

It is not what was expected.
And you are missing the result row.

You should comment out the line that runs the test suite, then work on fixing your code till you believe it is running properly then run the test suite at that point.

 python main.py
32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
============== test session starts ==============
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /home/runner/boilerplate-arithmetic-formatter-2/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter-2
collected 10 items

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

=================== FAILURES ====================
_____ 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         +  Error: Too many problems.
E         ? +                         +

test_module.py:77: AssertionError
____ test_template[test_incorrect_operator] _____

arguments = [['3 / 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = "Error: Operator must be '+' or '-'."
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be \'+\' or \'-\'."'

@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 a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."
E       assert " Error: Operator must be '+' or '-'. " == "Error: Operator must be '+' or '-'."
E         - Error: Operator must be '+' or '-'.
E         +  Error: Operator must be '+' or '-'.
E         ? +                                   +

test_module.py:77: AssertionError
______ test_template[test_too_many_digits] ______

arguments = [['24 + 85215', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = 'Error: Numbers cannot be more than four digits.'
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."'

@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 a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."
E       assert ' Error: Numbers cannot be more than four digits. ' == 'Error: Numbers cannot be more than four digits.'
E         - Error: Numbers cannot be more than four digits.
E         +  Error: Numbers cannot be more than four digits.
E         ? +                                               +

test_module.py:77: AssertionError
________ test_template[test_only_digits] ________

arguments = [['98 + 3g5', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = 'Error: Numbers must only contain digits.'
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."'

@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 a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."
E       assert ' Error: Numbers must only contain digits. ' == 'Error: Numbers must only contain digits.'
E         - Error: Numbers must only contain digits.
E         +  Error: Numbers must only contain digits.
E         ? +                                        +

test_module.py:77: AssertionError
============ short test summary info ============
FAILED test_module.py::test_template[test_too_many_problems]
FAILED test_module.py::test_template[test_incorrect_operator]
FAILED test_module.py::test_template[test_too_many_digits]
FAILED test_module.py::test_template[test_only_digits]
========== 4 failed, 6 passed in 0.19s ==========


Your code is still not producing any output line. And there is an extra space on the left of each operator. (And the dash line too)

I encourage you to comment out the line of code that runs the test suite and test your code manually until it is working properly so that when you turn the test suite back in you don’t have as many errors to fix.

Done, works very well Thank You

1 Like

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