The “Scientific Computing with Python” certification project “Arithmetic Formatter” is causing me some trouble, specifically in repl.it. After testing the code and ensuring it meets all the project reqs, I moved my code to repl.it and am getting these errors:
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/fcc-arithmetic-arranger-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 != ' 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/fcc-arithmetic-arranger-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 != ' 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`.
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=2)
My code is as follows:
def arithmetic_arranger(problems, display=False):
operators = []
longest_operands = []
len_diffs = []
num_spaces = []
if len(problems) > 5:
return "Error: Too many problems."
for problem in range(len(problems)):
if "+" in problems[problem]:
problems[problem] = problems[problem].split(" + ")
operators.append("+")
elif "-" in problems[problem]:
problems[problem] = problems[problem].split(" - ")
operators.append("-")
else:
return "Error: Operator must be '+' or '-'."
for problem in range(len(problems)):
if problems[problem][0].isdigit() == False:
return "Error: Numbers must only contain digits."
elif problems[problem][1].isdigit() == False:
return "Error: Numbers must only contain digits."
for problem in range(len(problems)):
if len(problems[problem][0]) > 4:
return "Error: Numbers cannot be more than four digits."
elif len(problems[problem][1]) > 4:
return "Error: Numbers cannot be more than four digits."
for problem in range(len(problems)):
if len(problems[problem][0]) < len(problems[problem][1]):
longest_operands.append("1")
len_diffs.append(1 + (len(problems[problem][1])-len(problems[problem][0])))
else:
longest_operands.append("-1")
len_diffs.append(-1 - (len(problems[problem][0])-len(problems[problem][1])))
#---------------------------------
for lens in range(len(len_diffs)):
if len_diffs[lens] > 0:
for num_times_print in range(len_diffs[lens]):
print(" ", end='')
print(" ",end='')
print(problems[lens][0], end='')
if lens != 3:
print(" ", end='')
else:
print(" ", end='')
print(problems[lens][0], end='')
if lens != 3:
print(" ", end='')
print("")
for lens in range(len(len_diffs)):
print(operators[lens], end='')
if len_diffs[lens] < 0:
for spaces in range(abs(len_diffs[lens])):
print(" ", end='')
print(problems[lens][1], end='')
if lens != 3:
print(" ", end='')
else:
print(" ", end='')
print(problems[lens][1], end='')
if lens != 3:
print(" ", end='')
print("")
for operands in range(len(longest_operands)):
if longest_operands[operands] == "-1":
for number_of_dashes in range(len(problems[operands][0])+2):
print("-", end='')
else:
for number_of_dashes in range(len(problems[operands][1])+2):
print("-", end='')
if operands != 3:
print(" ", end='')
print("")
if display == True:
solutions = []
for problem in range(len(problems)):
if operators[problem] == "+":
solutions.append(int(problems[problem][0]) + int(problems[problem][1]))
else:
solutions.append(int(problems[problem][0]) - int(problems[problem][1]))
for problem in range(len(problems)):
if len(problems[problem][0]) > len(problems[problem][1]):
num_spaces.append(len(str(problems[problem][0])) - len(str(solutions[problem])))
else:
num_spaces.append(len(str(problems[problem][1])) - len(str(solutions[problem])))
for spaces in range(len(num_spaces)):
for i in range(2 + int(num_spaces[spaces])):
print(" ", end='')
print(solutions[spaces], end='')
if spaces != 3:
print(" ", end='')
And here is the output when running the test case (on my IDE) that shows up in the errors:
Please advise on further steps and troubleshooting, TIA. Keep in mind that I have read the forums and concluded that this is not a problem with whitespace and newlines. If you need some code explanation, please feel free to ask.
Browser Information
Microsoft Edge Version 84.0.522.63 (Official build) (64-bit)