Need some help with Arithmetic Formatter

Hi,

I’m stuck with this project. I can’t see the error and why I’m not able to pass the tests, since everything seems to be correct.

You can see the project here. https://repl.it/join/oschujoz-robertogimenez

Please see my code.

def arithmetic_arranger(problems, other=False):
    primera_fila = ""
    segunda_fila = ""
    tercera_fila = ""
    resultado = ""
    arranged_problems = ""

    #print(problems)
    if (len(problems) > 5):
        arranged_problems = "Error: Too many problems."
    else:
        espacios = ""
        for i, problema in enumerate(problems):
            p = problema.split()
            try:
                n1 = int(p[0])
                n2 = int(p[2])
            except:
                arranged_problems = "Error: Numbers must only contain digits."
                return arranged_problems
            max_len = max(len(p[0]), len(p[2]))
            if (max_len > 4):
                arranged_problems = 'Error: Numbers cannot be more than four digits.'
                return arranged_problems
            #print(p)
            if ((p[1] == '+') or (p[1] == '-')):
                if (i != 0):
                    espacios = "      "
                if (p[1] == '+'):
                    suma = n1 + n2
                    esp = max_len + 2 - len(str(suma))
                    resultado = resultado + espacios + esp * " " + str(suma)
                else:
                    resta = n1 - n2
                    esp = max_len + 2 - len(str(resta))
                    resultado = resultado + espacios + esp * " " + str(resta)
                #print(resultado)
                if ((len(p[0]) != max_len) or (len(p[0]) == len(p[2]))):
                    primera_fila = primera_fila + espacios + (
                        "  " + (max_len - len(p[0])) * " " + str(p[0]))
                    segunda_fila = segunda_fila + espacios + (str(p[1]) + " " +
                                                              str(p[2]))
                else:
                    primera_fila = primera_fila + espacios + ("  " + str(p[0]))
                    segunda_fila = segunda_fila + espacios + (
                        str(p[1]) + " " +
                        (max_len - len(p[2])) * " " + str(p[2]))
                tercera_fila = tercera_fila + espacios + ((2 + max_len) * '-')
            else:
                arranged_problems = "Error: Operator must be '+' or '-'."
                return arranged_problems
        arranged_problems = primera_fila + '\n' + segunda_fila + '\n' + tercera_fila + '\n' + resultado + '\n'
        #print(arranged_problems)
        #print('fin')
    return arranged_problems

I’m passing the first test, but the second one is raising an error.
It’s weird that some strange chars are appearing after the second and third tests, like ‘F…F’
Any ideas?
Thanks!

Please provide a link to your project - so we can run it and see the error message ourselfs.
Right now I have no idea what those tests do, so giving feedback is kinda hard.

1 Like

Sure, thanks for the heads up.
I edited the post.

If I run your code, it fails the first and fourth tests (F…F…)

AssertionError: ’ 3 3801 45 123\n+ 855 -[104 chars]72\n’ != ’ 3 3801 45 123\n+ 855 - 2 [44 chars]----’

-     3        3801        45        123
?      --                --  --
+     3      3801      45      123
- + 855      -    2      + 43      +  49
?          --          --        --
+ + 855    -    2    + 43    +  49
- -----      ------      ----      -----
?      --              --    --         -
+ -----    ------    ----    ------   858        3799        88        172

: Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]

AssertionError: ’ 32 1 45 123\n- 698 - 38[101 chars]72\n’ != ’ 32 1 45 123\n- 698 - 3801 + [75 chars] 172’

-    32           1        45        123
?               -- --        --
+    32         1      45      123
- - 698      - 3801      + 43      +  49
?          --          --        --
+ - 698    - 3801    + 43    +  49
- -----      ------      ----      -----
?      --              --    --
+ -----    ------    ----    -----
-  -666       -3800        88        172
?      --                --  --         -
+  -666     -3800      88      172 

: Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithemetic problems and a second argument of True.

Does this point you in the right direction?

2 Likes

Yes, very inspirational.
Thanks!

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