Stuck on Arithmetic Formatter

I am stuck on the Arithmetic Formatter project. I have written the following code but cannot understand the source of failure.

CODE:

def arithmetic_arranger(problems, showAns = False):

  if len(problems)>5:
    return "Error: Too many problems."
  else:
    arranged_problems = []
    for i in problems:
      x = i.split()
      n1 = x[0]
      n2 = x[2]
      operand = x[1]

      if operand not in ["+", "-"]:
        return "Error: Operator must be '+' or '-'." 
      else:
        for digit in n1:
          if digit not in ["0","1","2","3","4","5","6","7","8","9"]:
            return "Error:  Numbers must only contain digits." 
          else: 
            continue

        for digit in n2:
          if digit not in ["0","1","2","3","4","5","6","7","8","9"]:
            return "Error: Numbers must only contain digits." 
          else: 
            continue

        if len(n1)>4 or len(n2)>4:
          return "Error: Numbers cannot be more than four digits."
        else: 

            if operand == '+':
              answer = int(n1) + int(n2)
            else: 
              answer = int(n1) - int(n2)
            
            width = max(len(n1),len(n2)) +2
            topLine = str(n1.rjust(width))
            secondLine =  str(operand + " " + n2)
            dashLine = str("-" * width)
            answerLine = str(answer).rjust(width)
            
            if showAns == True: 
              arranged_problems.append(topLine + "\n" + secondLine + "\n" + dashLine + "\n" + answerLine)
            else: 
              arranged_problems.append(topLine + "\n" + secondLine + "\n" + dashLine)


    return arranged_problems

Error Msg:

['   32\n+ 698\n-----', '  3801\n- 2\n------', '  45\n+ 43\n----', '  123\n+ 49\n-----']
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/fcc-arithmetic-arranger/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: ['    3\n+ 855\n-----', '  3801\n- 2\n---[42 chars]---'] != '    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/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: ['   32\n- 698\n-----\n -666', '     1\n-[73 chars]172'] != '   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.001s

FAILED (failures=2)

Thanks!

I can’t test it now, but what about passing simple arrays to test how your function is working?

Maybe add some console.log’ too.

I’m not too sure the first splitting is correct , so young can check that out.

can you give the link to your repl? so it is easier to debug

This is the result from simply printing arithmatic_arranger with the test values:

['   32\n+ 698\n-----', '  3801\n- 2\n------', '  45\n+ 43\n----', '  123\n+ 49\n-----']

it appears split seems to be working correctly

Sure! here it is:

https://repl.it/@VivekRadhakris1/fcc-arithmetic-arranger#arithmetic_arranger.py

you are returning an array of strings, you need to return a single string

Omg thank you! that solved it!