My code runs just fine in my compiler, but the test modules are giving me these errors:
I can’t figure it out.
Code is below.
I appreciate any feedback y’all may have
FAIL: test_arrangement (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-2/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: (None, None, None) != ’ 3 3801 45 123\n+ 855 [56 chars]----’ : 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 “/home/runner/boilerplate-arithmetic-formatter-2/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 arithemetic problems and a second argument of True.’)
AssertionError: (None, None, None, None) != ’ 32 1 45 123\n- 698 [90 chars] 172’ : Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithemetic problems and a second argument of True.
def arithmetic_arranger(problems, answer=False):
if len(problems) > 5:
error = "Too many problems! 5 At most!"
return error
else:
line1 = []
line2 = []
line3 = []
line4 = []
quad_space = " "
for p in problems:
item = p.split()
num_1 = item[0]
num_2 = item[2]
operand = item[1]
sum = ""
if len(num_1) > 4 or len(num_2) > 4:
error = "One or more numbers are too long. 4 Digits MAX"
return error
# check if all digits.
for digit in num_1, num_2:
if not digit.isdigit():
error = "Only numbers!"
return error
if operand not in ["+", "-"]:
error = "Invalid operand"
return error
if operand == "+":
sum = int(num_1) + int(num_2)
elif operand == "-":
sum = int(num_1) - int(num_2)
list = [num_1, num_2]
max_el_length = len(max(list))
#length of longest number in set
# max_el_length + 2 = number of dashes
top_line = f"{num_1:>{max_el_length + 2}}"
dashed_line = "-" * (max_el_length + 2)
bottom_line_spaces = len(dashed_line) - len(num_2) - 2
bottom_line = operand + " " + (" " * bottom_line_spaces) + num_2
sum_line_spaces = (max_el_length + 2) - len(str(sum))
sum_line = (" " * sum_line_spaces) + str(sum)
line1.append(top_line + quad_space)
line2.append(bottom_line + quad_space)
line3.append(dashed_line + quad_space)
line4.append(sum_line + quad_space)
#removes extra quad_space from the end of each last item in line
line1[-1] = (line1[-1])[:-5]
line2[-1] = (line2[-1])[:-5]
line3[-1] = (line3[-1])[:-5]
line4[-1] = (line4[-1])[:-5]
i = ""
j = ""
k = ""
l = ""
for item in line1:
i += item
for item in line2:
j += item
for item in line3:
k += item
for item in line4:
l += item
if answer == False:
arranged_problems = print(i), print(j), print(k)
else:
arranged_problems = print(i), print(j), print(k), print(l)
return arranged_problems