Arithmetic formatter

Hi,
Could you please hep me to assess my codes too? Been stalked for days and I really don’t know what to do because I am new at this.

here’s my code:

def arithmetic_arranger(problems, solve = False) :
    if len(problems) > 5 :
      return "Error: Too many problems."
    num_1 = ""
    operator = ""
    num_2 = ""
    lines = ""
    total = ""
    arranged_problems = ""

  #expected_output = ['3801 - 2', '123 + 49']],
       # '  3801      123\n'
       # '-    2    +  49\n'
       # '------    -----',
    for problem in problems :
      num1 = problem.split(" ")[0]
      operator = problem.split(" ")[1]
      num2 = problem.split(" ")[2]
      
      if not num1.isnumeric() or not num2.isnumeric() :
        return "Error: Numbers must only contain digits."

      if len(num1) >= 5 or len(num2) >= 5 :
        return "Error: Numbers cannot be more than four digits."

      if (operator == '+' or operator == '-') :
        if (operator == '+') :
          sum = str(int(num1) + int(num2))
          
        else :
          sum = str(int(num1) - int(num2))
        length = max(len(num1), len(num2)) + 2
        row1 = str(num1).rjust(length) 
        row2 = operator + str(num2).rjust(length-1)
        line = ""
        output = str(sum).rjust(length)
        
        for item in range(length) :
          line += "-"
          
        if problem != problems[-1] :
          num_1 += row1 + "    "
          num_2 += row2 + "    "
          lines += line + "    "
          total += output + "    "

        else :
          num_1 += row1
          num2 += row2
          lines += line
          total += output

      else :
        return "Error: Operator must be '+' or '-'."
    
      if solve :
        arranged_problems = 'num_1\n' + 'num_2\n' + 'lines\n' +total

      else :
        arranged_problems = 'num_1\n' + 'num_2\n' + lines
  
      return arranged_problems

https://replit.com/@OluwatosinBamig/boilerplate-arithmetic-formatter-7#arithmetic_arranger.py

here’s the error message I am getting:

      AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E       assert 'num_1\nnum_2\nlines\n -666    ' == ('   32         1      45      123      988\n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028')
E         -    32         1      45      123      988
E         - - 698    - 3801    + 43    +  49    +  40
E         - -----    ------    ----    -----    -----
E         -  -666     -3800      88      172     1028
E         + num_1
E         + num_2
E         + lines
E         +  -666

test_module.py:77: AssertionError
=========================== short test summary info ============================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - Assert...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - Assert...
FAILED test_module.py::test_template[test_four_problems_arrangement] - Assert...
FAILED test_module.py::test_template[test_five_problems_arrangement] - Assert...
FAILED test_module.py::test_template[test_two_problems_with_solutions] - Asse...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - Ass...
========================= 6 failed, 4 passed in 0.30s ==========================


For the first test case, when you see:

assert 'num_1\nnum_2\n------    ' == '  3801      123\n-    2    +  49\n------    -----'

This means, your result:

num_1\nnum_2\n------    

should be:

  3801      123\n-    2    +  49\n------    -----

Hi Randell,

Thanks for your response.

Are you trying to say my code is good except for last 3 lines?

Also, am I supposed to assert it like this:
‘num_1\nnum_2\nlines\n’ as you mentioned up there? It’s becoming a little confusing to me.

Fixing the last 3 lines will solve some of your problems and allow you to pass some of the test cases. However, you still have at least one other issue which needs resolution to pass all of the test cases.

Okay, thank you!

I’ll look into it.

Hi Randell,

Thanks for your help!

I finally got it, though I had to re-write it from scratch.