# Scientific Computing with Python - Arithmetic Arranger

Hey guys, I need help with this assignment. I think my problem is the result of the math kept overriding the previous one. How do I stop it? I need the previous result and the next result to be recorded under the lines and I don’t know how.

Here’s my code:

``````def arithmetic_arranger(problems, solve = False):
# limit is five
if len(problems) > 5:
return "Error: Too many problems."

firstLine = ""
secondLine = ""
equal = ""
total = ""
string = ""

for argument in problems:
firstNumber = argument.split(" ")[0]
operand = argument.split(" ")[1]
secondNumber = argument.split(" ")[2]

if operand != "+" and operand != "-":
return "Error: Operator must be '+' or '-'."
#  number operand should only contain digits
if not firstNumber.isdigit() or not secondNumber.isdigit():
return "Error: Numbers must only contain digits."
#  each operand has the max of 4 digits in width
if len(firstNumber) >= 5 or len(secondNumber) >= 5:
return "Error: Numbers cannot be more than four digits."

if operand == "+":
total = str(int(firstNumber) + int(secondNumber))
elif operand == "-":
total = str(int(firstNumber) - int(secondNumber))

# get the longest distance
operatorDistance = max(len(firstNumber), len(secondNumber)) + 2

secondNumber = operand + secondNumber.rjust(operatorDistance - 1)
firstLine = firstLine + firstNumber.rjust(operatorDistance) + (4 * " ")
secondLine = secondLine + secondNumber + (4 * " ")
equal = equal + len(secondNumber) * "_" + (4 * " ")
total = total.rjust(operatorDistance) + (4 * " ")

if solve:
string = firstLine + "\n" + secondLine + "\n" + equal + "\n" + total
print(string)
``````

The test

``````None
==================================== 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/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
------------------------------------ Captured stdout call ------------------------------------
3
+ 855
_____
858
3      988
+ 855    +  40
_____    _____
1028
______________________ 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
------------------------------------ Captured stdout call ------------------------------------
32
- 698
_____
-666
32         1
- 698    - 3801
_____    ______
-3800
32         1      45
- 698    - 3801    + 43
_____    ______    ____
88
32         1      45      123
- 698    - 3801    + 43    +  49
_____    ______    ____    _____
172
32         1      45      123      988
- 698    - 3801    + 43    +  49    +  40
_____    ______    ____    _____    _____
1028
================================== short test summary info ===================================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - AssertionError: Expe...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - AssertionError: Expe...
FAILED test_module.py::test_template[test_four_problems_arrangement] - AssertionError: Expe...
FAILED test_module.py::test_template[test_five_problems_arrangement] - AssertionError: Expe...
FAILED test_module.py::test_template[test_two_problems_with_solutions] - AssertionError: Ex...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - AssertionError: E...
================================ 6 failed, 4 passed in 0.22s =================================
``````

You should not be printing the string.
You should create a single string with all the output in it and return it.

I created a list an iterated through it.

Research the eval() method in python. When you enter a sum as a string, the method will evaluate the expression and return the answer. It is what I did yesterday.

I think my problem is with the spacing for the result. I tried to return the string. It went with the extra ++++ space in the test. I have tried using eval() for the total and print the result, the math seems to be correct. But the spacing is not and I don’t understand why the errors give a duplicate line of math. Any additional helps would be great!

``````def arithmetic_arranger(problems, solve = False):
# limit is five
if len(problems) > 5:
return "Error: Too many problems."

firstLine = ""
secondLine = ""
equal = ""
sum = ""
string = ""

for argument in problems:
firstNumber = argument.split(" ")[0]
operand = argument.split(" ")[1]
secondNumber = argument.split(" ")[2]

if operand != "+" and operand != "-":
return "Error: Operator must be '+' or '-'."
#  number operand should only contain digits
if not firstNumber.isdigit() or not secondNumber.isdigit():
return "Error: Numbers must only contain digits."
#  each operand has the max of 4 digits in width
if len(firstNumber) >= 5 or len(secondNumber) >= 5:
return "Error: Numbers cannot be more than four digits."

if operand == "+":
total = eval(str(int(firstNumber) + int(secondNumber)))
elif operand == "-":
total = eval(str(int(firstNumber) - int(secondNumber)))
print("subtraction", total)

# get the longest distance
operatorDistance = max(len(firstNumber), len(secondNumber)) + 2

secondNumber = operand + secondNumber.rjust(operatorDistance - 1)
firstLine = firstLine + firstNumber.rjust(operatorDistance) + (4 * " ")
secondLine = secondLine + secondNumber + (4 * " ")
equal = equal + len(secondNumber) * "_" + (4 * " ")
sum = str(total).rjust(operatorDistance) + (4 * " ")

if solve:
string = firstLine + "\n" + secondLine + "\n" + equal + "\n" + sum
else:
string = firstLine + "\n" + secondLine + "\n" + equal
return string
``````
``````addition 730
subtraction 3799
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/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         +   3801      123
E         ?                ++++
E         - -    2    +  49
E         + -    2    +  49
E         ?                ++++
E         - ------    -----
E         + ______    _____

test_module.py:77: AssertionError
--------------------------------------- Captured stdout call ----------------------------------------
subtraction 3799
___________________________ 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         +   1         1
E         ?              ++++
E         - + 2    - 9380
E         + + 2    - 9380
E         ?              ++++
E         - ---    ------
E         + ___    ______

test_module.py:77: AssertionError
--------------------------------------- Captured stdout call ----------------------------------------
subtraction -9379
___________________________ 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         +     3      3801      45      123
E         ?                                 ++++
E         - + 855    -    2    + 43    +  49
E         + + 855    -    2    + 43    +  49
E         ?                                 ++++
E         - -----    ------    ----    -----
E         + _____    ______    ____    _____

test_module.py:77: AssertionError
--------------------------------------- Captured stdout call ----------------------------------------
subtraction 3799
___________________________ 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         +   11      3801      1      123         1
E         ?                                         ++++
E         - +  4    - 2999    + 2    +  49    - 9380
E         + +  4    - 2999    + 2    +  49    - 9380
E         ?                                         ++++
E         - ----    ------    ---    -----    ------
E         + ____    ______    ___    _____    ______

test_module.py:77: AssertionError
--------------------------------------- Captured stdout call ----------------------------------------
subtraction 802
subtraction -9379
__________________________ 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_____    _____     1028    ' == '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
E         -     3      988
E         +     3      988
E         ?               ++++
E         - + 855    +  40
E         + + 855    +  40
E         ?               ++++
E         + _____    _____     1028
E         - -----    -----
E         -   858     1028

test_module.py:77: AssertionError
--------------------------------------- Captured stdout call ----------------------------------------
_________________________ 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 '_____    ______    ____    _____    _____     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         + - 698    - 3801    + 43    +  49    +  40
E         ?                                          ++++
E         + _____    ______    ____    _____    _____     1028
E         - -----    ------    ----    -----    -----
E         -  -666     -3800      88      172     1028

test_module.py:77: AssertionError
--------------------------------------- Captured stdout call ----------------------------------------
subtraction -666
subtraction -3800
====================================== short test summary info ======================================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - AssertionError: Expected di...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - AssertionError: Expected di...
FAILED test_module.py::test_template[test_four_problems_arrangement] - AssertionError: Expected di...
FAILED test_module.py::test_template[test_five_problems_arrangement] - AssertionError: Expected di...
FAILED test_module.py::test_template[test_two_problems_with_solutions] - AssertionError: Expected ...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - AssertionError: Expected...
==================================== 6 failed, 4 passed in 0.68s ====================================
``````

Right, that is saying you have too much space at the end of the row. How could you prevent that?