 # Arithmetic arranger help with code

Hello everyone,
I am stuck with the arithmetic_arranger. I am trying to refactor my code into different functions to make it cleaner, but it seems that there is some issue that I cannot see. Therefore, I would like to ask you for some help on how to refactor it.

Thank you!

Here is the code I wrote:

``````MAX_PROBLEMS = 5
MAX_LENGTH = 4

# Define variables
top_line = processing(problems)
bottom_line = processing(problems)
dashes = processing(problems)
results = processing(problems)
arranged_problems = ""

# Display the results if answer is True
arranged_problems = '\n'.join((top_line, bottom_line, dashes))
else:
arranged_problems = '\n'.join((top_line, bottom_line, dashes, results))

return arranged_problems

def error_handling(problems, first, operator, second):
"""Function that handles the errors of the Arithmetic Arranger."""

# Define what happens if len(problems) is larger than 5
if len(problems) > MAX_PROBLEMS:
return "Error: Too many problems."

# Handle length of operands larger than 4
if len(first) > MAX_LENGTH or len(second) > MAX_LENGTH:
return "Error: Numbers cannot be more than four digits."

# Handle no digits in the operands
elif first.isdigit() == False or second.isdigit() == False:
return "Error: Numbers must only contain digits."

# Handle wrong operators, else calculate the results
elif operator not in ['+','-']:
return "Error: Operator must be '+' or '-'."

# If no errors
else:
return "No errors."

def processing(problems):
"""Function that processes the problems from the Arithmetic Arranger into the adequate format."""

# Define variables
top_list = []
bottom_list = []
dashes = []
results = []

# Define empty spaces
empty_space = " " * MAX_LENGTH

# Extract each problem from the list and split it in three parts
for problem in problems:
first, operator, second = problem.split(" ")

# Pass error_handling function
error = error_handling(problems, first, operator, second)
if error != "No errors.":
return error

# Calculate the maximum width between operands
width = max(len(first), len(second)) + 2

# Build top line
top_list.append(first.rjust(width))

# Build bottom line
bottom_list.append(operator + second.rjust(width - 1))

# Build dashes
dashes.append('-' * width)

# Calculate the results for '+' or '-'
if operator == '+':
calculations = str(int(first) + int(second))
elif operator == '-':
calculations = str(int(first) - int(second))

# Build results list
results.append(calculations.rjust(width))

# Add empty spaces to the lines
top_list = empty_space.join(top_list)
bottom_list = empty_space.join(bottom_list)
dashes = empty_space.join(dashes)
results = empty_space.join(results)

``````

The error I am getting is the following:

`````` python main.py
32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
.FF.FF
======================================================================
FAIL: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 24, in test_incorrect_operator
self.assertEqual(actual, expected, '''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''')
AssertionError: 'E\nr\nr' != "Error: Operator must be '+' or '-'."
+ Error: Operator must be '+' or '-'.- E
- r
- r : Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."

======================================================================
FAIL: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 34, in test_only_digits
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."')
AssertionError: 'E\nr\nr' != 'Error: Numbers must only contain digits.'
+ Error: Numbers must only contain digits.- E
- r
- r : Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."

======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 29, in test_too_many_digits
self.assertEqual(actual, expected, '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."')
AssertionError: 'E\nr\nr' != 'Error: Numbers cannot be more than four digits.'
+ Error: Numbers cannot be more than four digits.- E
- r
- r : 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."

======================================================================
FAIL: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 19, in test_too_many_problems
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."')
AssertionError: 'E\nr\nr' != 'Error: Too many problems.'
+ Error: Too many problems.- E
- r
- r : Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."

----------------------------------------------------------------------
Ran 6 tests in 0.002s

FAILED (failures=4)
``````

Note how `E\nr\nr` would be the result of joining the first three characters of `Error:...` with newlines (you would get `E\nr\nr\no` if `answer` were `True` in any of these tests). You are returning the error message from the error finder to your processing function to your arranger function and it’s treating the message as if it were lines since strings are arrays too.

Yes, you are right, thanks! Certainly, the problem comes when I return the error. But, I cannot find a way to avoid passing it to the arithmetic formatter, unless I use a if/else statement So, I finally solved the issue. Thank you for your help!