Arithmetic Formatter | test_arrangement and test_solutions failed

I am not sure what I am doing wrong here :confused: 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)

You’ve not formatted your answer correctly… There are unnecessary spaces
It would be good if you provide us your repl link, So that we can run and check your output

Thanks for the quick response.

Sure, here is the link:
https://repl.it/@OliverRehbein/arithmeticformatter#arithmetic_arranger.py

Check what are you doin here…
For each problem in problem list, you are storing the output in output variable

But for each iteration, you’re overwriting the value of output variable
Hence, in first iteration it contents, first problem in above format, then second problem and at last it is returning the last problem in above format…

I think, you’ve to think it other way,

Store first operand of each problem in a variable,
second operand in another variable,
third variable should contain, operators
and 4th will have solution…(You can j=use 4th variable for separator line and 5th for solution)
Then you can return above variables separated by newline
I’ve tried using above algorithm

Fixed it! Thank you so much.

I didn´t think about the overwriting in the for-loop.