# Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
My code outputs the way it was asked to on my machine. But when running on the replit website, it doesn’t pass the tests… Why is that?

``````def arithmetic_arranger(array, boolean=False):
# checking for errors
if len(array) > 5:
return "Error: Too many problems."

lines = list()

# looping though array elements
for item in array:
# checking for errors
if '+' in item or '-' in item:
if '+' in item:
index = item.find('+')
sign = '+'

elif '-' in item:
index = item.find('-')
sign = '-'

line_1 = item[:(index - 1)].strip()
line_2 = item[(index + 1):].strip()
else:
return "Error: Operator must be '+' or '-'."

if not line_1.isdigit() or not line_2.isdigit():
return "Error: Numbers must only contain digits."

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

# operations result
operand_1 = int(line_1)
operand_2 = int(line_2)

if sign == '+':
result = operand_1 + operand_2
result = str(result)
elif sign == '-':
result = operand_1 - operand_2
result = str(result)

# for the graphical interface
longest_operand_length = 0

if len(line_1) >= len(line_2):
longest_operand_length = len(line_1)
else:
longest_operand_length = len(line_2)

dashes = '-' * (longest_operand_length + 2)

lines.append([line_1, sign, line_2, dashes, result])

# graphical interface
first_line_array = list()
second_line_array = list()
third_line_array = list()
forth_line_array = list()

first_line = str()
second_line = str()
third_line = str()
forth_line = str()

# building first line
for line in lines:
first_line_array.append(
(" " * (len(line[3]) - len(line[0]))) + line[0] + "    "
)

for line in first_line_array:
first_line += line

# building second line
for line in lines:
second_line_array.append(
line[1] + (" " * (len(line[3]) - len(line[2]) - 1)) + line[2] + "    "
)

for line in second_line_array:
second_line += line

#building third line
for line in lines:
third_line_array.append(line[3] + "    ")

for line in third_line_array:
third_line += line

#building forth line
for line in lines:
forth_line_array.append(
(" " * (len(line[3]) - len(line[4]))) + line[4] + "    "
)

for line in forth_line_array:
forth_line += line

# building all lines together
if boolean:
all_lines = f"{first_line}\n{second_line}\n{third_line}\n{forth_line}"
else:
all_lines = f"{first_line}\n{second_line}\n{third_line}"

return all_lines

```python

User Agent is: <code>Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36</code>

**Challenge:**  Scientific Computing with Python Projects - Arithmetic Formatter

https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter``````
1 Like

What do the errors say?

At the replit website, this output is shown:

``````python main.py
32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
==================== 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] 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         + ------    -----
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         +   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 ('    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         + -----    ------    ----    -----
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         +   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_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         +     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 ('   32         1      45      123      988    \n'\n '- 698    - 3801    + 43    +  49    +  40    \n'\n '-----    ------    ----    -----    -----    \n'\n ' -666     -3800      88      172     1028    ') == ('   32         1      45      123      988\n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028')
E         -    32         1      45      123      988
E         +    32         1      45      123      988
E         ?                                          ++++
E         - - 698    - 3801    + 43    +  49    +  40
E         + - 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]
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.21s ===============
``````

On my computer, this is the output:

``````python main.py
32         1      9999      523
+  8    - 3801    + 9999    -  49
----    ------    ------    -----
40     -3800     19998      474
``````

You are adding an extra 5 space characters at the end of each line (except the last one) and you should not be doing that to match the expected string value.

You can see it when I select your output above:

1 Like

The errors from the test suite try to tell you this. You should see lines with + and - showing lines of output you have (+) that are not expected and lines of output that are expected (-) but you don’t have.

2 Likes

Uh, yes, I can see it. I didn’t know it would happen. Thank you so much, sir!

1 Like

Don’t feel too bad. This appears to be the most common mistake on this project.

2 Likes

This translates to

``````-   3801      123
+   3801      123
?                ++++
- -    2    +  49
+ -    2    +  49
?                ++++
- ------    -----
+ ------    -----
?                ++++
``````
1 Like

I see, I understand now, sir. I thought it just had to look like as shown on the challenge’s page. Thank you so much.

2 Likes