I am getting the correct output, however, it shows two errors for me. I am unsure what I amdoing wrong
My Code
def arithmetic_arranger(problems, display_answer=True):
answer = []
first_string = ''
ans = ''
second = ''
dash = ''
if len(problems) > 5:
return "Error: Too many problems."
for li in problems:
values = li.split(' + ')
values1 = li.split(' - ')
if "+" in li:
add_values = li.split(' + ')
max_add = max(len(add_values[0]), len(add_values[1]))
min_add = min(len(add_values[0]), len(add_values[1]))
if len(add_values[0]) > 4 or len(add_values[1]) > 4:
return "Error: Numbers cannot be more than four digits."
if not add_values[0].isdigit() or not add_values[1].isdigit():
return "Error: Numbers must only contain digits."
# first_string += " "
if len(add_values[0]) >= len(add_values[1]):
first_string += ' ' * ((max_add + 2) - max_add)
first_string += add_values[0]
else:
first_string += ' ' * ((max_add + 2) - min_add)
first_string += add_values[0]
second += "+"
second += " " * ((max_add + 2) - 1 - len(add_values[1]))
second += add_values[1]
answer.append(int(add_values[0]) + int(add_values[1]))
elif "-" in li:
sub_values = li.split(' - ')
max_sub = max(len(sub_values[0]), len(sub_values[1]))
min_sub = min(len(sub_values[0]), len(sub_values[1]))
if len(sub_values[0]) > 4 or len(sub_values[1]) > 4:
return "Error: Numbers cannot be more than four digits."
if not sub_values[0].isdigit() or not sub_values[1].isdigit():
return "Error: Numbers must only contain digits."
# first_string += " "
if len(sub_values[0]) >= len(sub_values[1]):
first_string += ' ' * ((max_sub + 2) - max_sub)
first_string += sub_values[0]
else:
first_string += ' ' * ((max_sub + 2) - min_sub)
first_string += sub_values[0]
second += "-"
second += " " * ((max_sub + 2) - 1 - len(sub_values[1]))
second += sub_values[1]
answer.append(int(sub_values[0]) - int(sub_values[1]))
else:
return "Error: Operator must be '+' or '-'."
# raise SyntaxError("Operator must be '+' or '-'.")
first_string += " " * 4
second += " " * 4
ans_space = []
for li in problems:
if "+" in li:
add_values = li.split(' + ')
max_add = max(len(add_values[0]), len(add_values[1]))
min_add = min(len(add_values[0]), len(add_values[1]))
dash += "-" * (max_add + 2)
ans_space.append(max_add + 2)
elif "-" in li:
sub_values = li.split(' - ')
max_sub = max(len(sub_values[0]), len(sub_values[1]))
min_sub = min(len(sub_values[0]), len(sub_values[1]))
dash += "-" * (max_sub + 2)
ans_space.append(max_sub + 2)
dash += " " * 4
first_string += "\n"
second += "\n"
dash += "\n"
for i in range(0, len(answer)):
ans += " " * (ans_space[i] - len(answer[i].__str__()))
ans += f"{answer[i]}"
ans += " " * 4
if display_answer:
return first_string + second + dash + ans
else:
return first_string + second + dash
My Error
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\patel\Desktop\Python\Projects\Arithmetic Formatter\test_module.py", line 10, in test_arrangement
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: ' [23 chars] 123 \n+ 855 - 2 + 43 + 49 [73 chars] ' != ' [23 chars] 123\n+ 855 - 2 + 43 + 49\n-----[23 chars]----'
- 3 3801 45 123
? ----
+ 3 3801 45 123
- + 855 - 2 + 43 + 49
? ----
+ + 855 - 2 + 43 + 49
- ----- ------ ---- -----
? -----
+ ----- ------ ---- ------ 858 3799 88 172 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\patel\Desktop\Python\Projects\Arithmetic Formatter\test_module.py", line 39, in test_solutions
self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: ' 3[23 chars] 123 \n- 698 - 3801 + 43 + 49 [73 chars] ' != ' 3[23 chars] 123\n- 698 - 3801 + 43 + 49\n-----[57 chars] 172'
- 32 1 45 123
? ----
+ 32 1 45 123
- - 698 - 3801 + 43 + 49
? ----
+ - 698 - 3801 + 43 + 49
- ----- ------ ---- -----
? ----
+ ----- ------ ---- -----
- -666 -3800 88 172 ? ----
+ -666 -3800 88 172 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.
----------------------------------------------------------------------
Ran 6 tests in 0.004s
FAILED (failures=2)
Process finished with exit code 0