My output format is not matching the one tested against for FreeCodeCamp Python challenge

I am trying to wrap up a FreeCodeCamp course which includes creating a program for a project. This program involves formatting arithmetic problems a certain way; I feel as though describing the project in detail is not too important because the error stems from formatting. What they expect can be found in the error message. My output seems nearly identical to the expected output that the project requires, but I have no idea as to why it is saying my code is incorrect. My code works fine yet it doesnt match EXACTLY what is expected… feel like I’m wasting my time so I turned to you all. I tried deciphering their error message–could some one explain what error this message is actually describing and how I can fix it? If you do need further info, let me know and I can provide it

My code:

def arithmetic_arranger(problems, solve=False):
  if len(problems) > 5:
    return "Error: Too many problems."
  top_numbers = ''
  bottom_numbers = ''
  dashes = ''
  solutions = ''
  for problem in problems:
    split_terms = problem.split(' ')
    equation_length = max([(len(x)+2) for x in split_terms])
    term_1_length = len(split_terms[0])
    term_2_length = len(split_terms[2])
    if len(split_terms) != 3:
      return 'Please format probems as "x + y" with spaces between numbers and operators'
    if split_terms[1] not in ['+', '-']:
      return "Error: Operator must be '+' or '-'."
    # if type(split_terms[0]) != int or type(split_terms[2]) != int:
    try:
      int(split_terms[0])
      int(split_terms[2])
    except:
      return "Error: Numbers must only contain digits." 
    if term_1_length > 4 or term_2_length > 4:
      return "Error: Numbers cannot be more than four digits."
    solution = str(eval(problem))
    solution_length = len(str(solution))
    # if len(split_terms[0]) < len(split_terms[2]):
    whitespaces_1 = (equation_length - term_1_length) * ' '
    whitespaces_2 = (equation_length - term_2_length - 1) * ' '
    operator = split_terms[1]
    dash_len = '-'*(equation_length)
    top_numbers += whitespaces_1 + split_terms[0] + '    '
    bottom_numbers += str(operator) + whitespaces_2 + split_terms[2] + '    '
    dashes += dash_len + '    '
    solutions += ((equation_length - solution_length) * ' ') + solution + '    '
  arranged_problems = top_numbers + 'n' + bottom_numbers + 'n' + dashes
  if solve == True:
    arranged_problems += 'n' + solutions
  return arranged_problems

The error message I have received:

________________ test_template[test_two_problems_arrangement1] _________________

arguments = [['3801 - 2', '123 + 49']]
expected_output = '  3801      123n-    2    +  49n------    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'

    @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 different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    n-    2    +  49    n------    -----    ' == '  3801      123n-    2    +  49n------    -----'
E         -   3801      123↔
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         + -    2    +  49    
E         ?                ++++
E         - ------    -----
E         + ------    -----    
E         ?                ++++

test_module.py:77: AssertionError

Answer

Their expected output is:

"  3801      123n-    2    +  49n------    -----"

Your actual output is:

"  3801      123    n-    2    +  49    n------    -----    "

Note that each of your lines of text, immediately before the n and end of string, includes 4 space characters. If you deleted those, they would match.

You have added an extraneous ' ' blank string at the end of each term, including the last one on each line. So your output doesn’t match the desired output, which does not have extraneous terminating whitespace.

Change your code to strip the terminating whitespace from each line before aggregating the results into arranged_problems, or simply don’t append the whitespace after the last term on each line.

One quick way to solve this is to change this:

arranged_problems = top_numbers + "n" + bottom_numbers + "n" + dashes

to this:

arranged_problems = top_numbers.rstrip() + "n" + bottom_numbers.rstrip() + "n" + dashes.rstrip()