Arithmetic Formatter - test_arrangement fail

Hey! I’ve started learning to code recently, and after a day of intense frustration, i’v almost finally made it to the end of the first python challenge except for this.

The error test_arrangement keep coming up. I’ve tried to redo my dash count to be sure, as it seemed to be the main focus of the error to be sure but I just can’t wrap my head around it.

Also, I was asking myself why on repl.it the return keyword (or function?) produce an output whereas it produce nothing on my PC. I’ve downgraded my python to 3.8.2 to be sure, alternated between with or without parentheses,… So if someone feel extra-special and feel like helping me on this one, I would also very grateful.

Below, you’ll find (in order): the Repl link, my code, the error message.

Thanks for reading and for all the help you can offer. :slight_smile:

The Repl link

Here is my code so far

def arithmetic_arranger(problems, result=False):
    a = []
    fline = []
    sline = []
    dline = []
    rline = []

    if len(problems) > 4:
        #print('Error: Too many problems.')
        return('Error: Too many problems.')

    for x in problems:
        a = x.split(' ')

        fterm = a[0]
        sterm = a[2]
        ope = a[1]

        #Error check

        if len(fterm) > 4 or len(sterm) > 4:
            #print('Error: Numbers cannot be more than four digits.')
            return ("Error: Numbers cannot be more than four digits.")

        if x.find("/") != -1:
            #print("Error: Operator must be '+' or '-'.")
            return ("Error: Operator must be '+' or '-'.")
        if x.find("*") != -1:
            #print("Error: Operator must be '+' or '-'.")
            return ("Error: Operator must be '+' or '-'.")

        if fterm.isdigit() is not True:
            #print("Error: Numbers must only contain digits.")
            return ("Error: Numbers must only contain digits.")
        if sterm.isdigit() is not True:
            #print("Error: Numbers must only contain digits.")
            return ("Error: Numbers must only contain digits.")

        #Sum

        if result is True:
            if ope == '-':
                sum = int(fterm) - int(sterm)
            if ope == '+':
                sum = int(fterm) + int(sterm)
          

        #Formatting

        if len(fterm) > len(sterm):
            dash = len(fterm) + 2
        else:
            dash = len(sterm) + 2

        fspace = dash - len(fterm)
        sspace = dash - len(sterm) - 1

        fline.append(fspace * " " + fterm)
        sline.append(ope + sspace * " " + sterm)
        dline.append(dash * '-')

        if result is True:
            rspace = dash - len(str(sum))
            rline.append(rspace * " " + str(sum))

    arranged_problems = ("    ".join(fline) + "\n" + "    ".join(sline) +
                         "\n" + "    ".join(dline) + "\n")
    if result is True:
        arranged_problems = arranged_problems + ("    ".join(rline))

    #print(arranged_problems)
    return arranged_problems


The complet error message

FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-1/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: '    [36 chars]   -    2    + 43    +  49\n-----    ------    ----    -----\n' != '    [36 chars]   -    2    + 43    +  49\n-----    ------    ----    -----'
      3      3801      45      123
  + 855    -    2    + 43    +  49
- -----    ------    ----    -----
?                                 -
+ -----    ------    ----    ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

----------------------------------------------------------------------

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.1 Safari/605.1.15.

Challenge: Arithmetic Formatter

Link to the challenge:

Welcome to the forums @Gustave.

If you reformat your error message, you’ll see the problem:

unittest is also trying to tell you here:

where the - minus line is your output, the + line is the expected, and the ? line is the difference, but it’s hard to understand that it’s telling you that you have an extra newline since the two appear identical.

Good luck.

Oh god thanks! Didn’t saw it at all!

Thanks a lot! :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.