Help with Arithmetic Formatter - Only returning one equation

Hello All,

I have been working on the Arithmetic Formatter project and struggling to return all the equations. It seems I have it formatted correctly for it will return with solution when asked but will only return the last equation from the array. I must be missing something obvious. Any thoughts?

def arithmetic_arranger(problems, solutions = False):
  if len(problems) > 5 : 
    return('Error: Too many problems.')
  
  line1 = ""
  line2 = ""
  line3 = ""
  line4 = ""
  arranged_problems = ''
  
  for problem in problems: 
    num1 = problem.split(" ")[0]
    operator = problem.split(" ")[1]
    num2 = problem.split(" ")[2]
    
    if not ((operator == "+") or (operator == "-")):
      return("Error: Operator must be '+' or '-'.")
    if num1.isnumeric() == False :
      return('Error: Numbers must only contain digits.')
    if num2.isnumeric() == False:
      return ('Error: Numbers must only contain digits.')
    if (len(num1) > 4) or (len(num2) > 4) : 
      return ('Error: Numbers cannot be more than four digits.')
    
    solution = ""
    if (operator == "+") : 
      solution = str(int(num1) + int(num2))
    elif (operator == "-") : 
      solution = str(int(num1) - int(num2))
  
    width = max(len(num1), len(num2)) + 2
    top = str(num1).rjust(width)
    bottom = operator + str(num2).rjust(width - 1)
    line = ""
    answer = str(solution).rjust(width)
    for x in range(width) : 
      line += "-"
    
    if problem != problems[-1] : 
      line1 += top + '    '
      line2 += bottom + '    '
      line3 += line + '    '
      line4 += answer + '    '
    else : 
      line1 = top
      line2 = bottom
      line3 = line
      line4 = answer

  if solutions == True : 
    arranged_problems = line1.rstrip() + "\n" + line2.rstrip() + "\n" + line3.rstrip() + "\n" + line4.rstrip()
  else : 
    arranged_problems = line1.rstrip() + "\n" + line2.rstrip() + "\n" + line3.rstrip()
    
  return arranged_problems

Error I am seeing when I run the code:


_______________ test_template[test_five_problems_with_solutions] _______________

arguments = [['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True]
expected_output = '   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       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 '  988\n+  40\n-----\n 1028' == '   32       ... 172     1028'
E         -    32         1      45      123      988
E         - - 698    - 3801    + 43    +  49    +  40
E         - -----    ------    ----    -----    -----
E         -  -666     -3800      88      172     1028
E         +   988
E         + +  40
E         + -----...
E         
E         ...Full output truncated (2 lines hidden), use '-vv' to show

test_module.py:77: AssertionError

Thank you in advance!

This means you are replacing the variable on the left every single iteration. Why not update each line instead and join the lines after the loop?

Thank you! Wow that was simple!
Thank you Again!!

 python main.py
   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
=========================== test session starts ===========================
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/runner/boilerplate-arithmetic-formatter-3
collected 10 items                                                        

test_module.py ..........                                           [100%]

=========================== 10 passed in 0.03s ============================
 
1 Like