I am not sure what I am doing wrong here Can you help me?
My code:
def arithmetic_arranger(problems, show_solution = False):
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
psplit = problem.split()
if psplit[1] != "+" and psplit[1] != "-":
return "Error: Operator must be '+' or '-'."
if not psplit[0].isnumeric() or not psplit[2].isnumeric():
return "Error: Numbers must only contain digits."
if len(psplit[0]) > 4 or len(psplit[2]) > 4:
return "Error: Numbers cannot be more than four digits."
operator = psplit[1]
operand_top = psplit[0]
operand_bottom = psplit[2]
max_operand_size = max(len(operand_top), len(operand_bottom))
if operator == "+":
solution = int(operand_top) + int(operand_bottom)
elif operator == "-":
solution = int(operand_top) - int(operand_bottom)
rjusted_operand_top = operand_top.rjust(max_operand_size)
rjusted_operand_bottom = operand_bottom.rjust(max_operand_size)
output_1 = " " + rjusted_operand_top + " "
output_2 = operator + " " + rjusted_operand_bottom + " "
output_3 = "-" * (max_operand_size + 2) + " "
output_4 = str(solution).rjust(max_operand_size +2) + " "
output = output_1 + "\n" + output_2 + "\n" + output_3 + "\n" + output_4
return(output)
Output:
python main.py
123
+ 49
-----
172
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/arithmeticformatter/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: ' 123 \n+ 49 \n----- \n 172 ' != ' 3 3801 45 123\n+ 855 [53 chars]----'
+ 3 3801 45 123
+ + 855 - 2 + 43 + 49
+ ----- ------ ---- ------ 123
- + 49
- -----
- 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 "/home/runner/arithmeticformatter/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: ' 123 \n+ 49 \n----- \n 172 ' != ' 32 1 45 123\n- 698 [87 chars] 172'
- 123
- + 49
- -----
- 172 + 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 arithemetic problems and a second argument of `True`.
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=2)