Can someone help me check where my code went wrong please?
I keep getting failures on test_solutions and test_arrangement
def arithmetic_arranger(problems, show = False):
firstAnswer = ""
operators = ""
dashlines = ""
secondAnswer = ""
sumup = ""
answer = ""
if(len(problems))>5:
return "Error: Too many problems."
for problem in problems:
#spliting the strings into a list
firstNumber = problem.split(" ")[0]
operators = problem.split(" ")[1]
secondNumber = problem.split(" ")[2]
# check the length of the number, max 4 digits
if (len(firstNumber) > 4 or len(secondNumber) > 4):
return "Error: Numbers cannot be more than four digits."
# check the input as valid digits
if not firstNumber.isnumeric() or not secondNumber.isnumeric():
return "Error: Numbers must only contain digits."
# check for the correct form of operators
if operators != "+" or "-":
return "Error: Operator must be '+' or '-'."
sum = ""
if operators == "+":
sum = str(int(firstNumber) + int(secondNumber))
elif operators == "-":
sum = str(int(firstNumber) - int(secondNumber))
length = max(len(firstNumber) , len(secondNumber)) + 2
fisrtline = str(firstNumber).rjust(length)
secondline = operators + str(secondNumber).rjust(length - 1)
sltn = str(sum.rjust(length))
dashline = ""
for dash in range(length):
dashline += "-"
if problem != problems[-1]:
firstAnswer += fisrtline + " "
secondAnswer += secondline + " "
dashlines += dashline + " "
sumup += sltn + " "
else:
firstAnswer += fisrtline
secondAnswer += secondline
dashlines += dashline
sumup += sltn
if show:
answer = firstAnswer + "\n" + secondAnswer + "\n" + dashlines + "\n" + sumup
else:
answer = firstAnswer + "\n" + secondAnswer + "\n" + dashlines
return answer
Here is the output:
python main.py
Error: Operator must be '+' or '-'.
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/FCC-Python-Project-10/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: "Error: Operator must be '+' or '-'." != ' 3 3801 45 123\n+ 855 [56 chars]----'
- Error: Operator must be '+' or '-'.
+ 3 3801 45 123
+ 855 - 2 + 43 + 49
----- ------ ---- -----
: 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-Python-Project-10/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: "Error: Operator must be '+' or '-'." != ' 32 1 45 123\n- 698 [90 chars] 172'
- Error: Operator must be '+' or '-'.
+ 32 1 45 123
- 698 - 3801 + 43 + 49
----- ------ ---- -----
-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.003s
FAILED (failures=2)