Hi all, I am new here and would like some help with the first project of Scientific Computing with Python. It is driving me nuts especially since I don’t understand the error feedback this time.
My project link: https://repl.it/join/pymkyytk-ryoukei
My code:
def arithmetic_arranger(problems, showAns = False):
if len(problems) > 5:
return "Error: Too many problems."
else:
topline = list()
midline = list()
botline = list()
final = list()
for problem in problems:
problem = problem.split()
n1 = problem[0]
operator = problem[1]
n2 = problem[2]
if operator not in ["+", "-"]:
return "Error: Operator must be '+' or '-'."
elif n1.isdigit() is False or n2.isdigit() is False:
return "Error: Numbers must only contain digits."
elif len(n1) > 4 or len(n2) > 4:
return "Error: Numbers cannot be more than four digits."
else:
if operator == "+":
answer = int(n1) + int(n2)
else:
answer = int(n1) - int(n2)
width = max(len(n1), len(n2)) + 2
top = str(n1.rjust(width))
mid = str(operator.ljust(width - max(len(n1), len(n2)) - 1) + n2.rjust(max(len(n1), len(n2)) + 1))
bottom = str("-" * width)
last = str(answer).rjust(width)
topline.append(top)
midline.append(mid)
botline.append(bottom)
final.append(last)
x = " ".join(topline)
y = " ".join(midline)
z = " ".join(botline)
a = " ".join(final)
if showAns == True:
return x + "\n" + y + "\n" + z + "\n" + a
else:
return x + "\n" + y + "\n" + z + "\n" + a
The error message I get is:
+ 855 - 2 + 43 + 49
----- ------ ---- -----
858 3799 88 172
F.....
======================================================================
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: ' [68 chars]- ------ ---- -----\n 858 3799 88 172' != ' [68 chars]- ------ ---- -----'
3 3801 45 123
+ 855 - 2 + 43 + 49
- ----- ------ ---- -----
? -
+ ----- ------ ---- ------ 858 3799 88 172 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
----------------------------------------------------------------------
Ran 6 tests in 0.003s
FAILED (failures=1)
Any help is appreciated! Thanks in advance.