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
def arithmetic_arranger(problems, answer = False):
# Define variables
top_line = processing(problems)[0]
bottom_line = processing(problems)[1]
dashes = processing(problems)[2]
results = processing(problems)[3]
arranged_problems = ""
# Display the results if answer is True
if answer == False:
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)
return top_list, bottom_list, dashes, 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)