Arithmetic Formatter: 1 Failure 1 Error

For the Python For Everybody Arithmetic Formatter project, I’m a little confused about what this one failure and error mean. Thanks in advance.

======================================================================
ERROR: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/boilerplate-arithmetic-formatter/test_module.py", line 37, in test_solutions
    actual = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True)
TypeError: arithmetic_arranger() takes 1 positional argument but 2 were given

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

----------------------------------------------------------------------
Ran 6 tests in 0.001s

FAILED (failures=1, errors=1)
   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
  730      3799      88      172

I think you may be outputting the solutions to the arithmetic problems when the test suite did not expect you to do so.

1 Like

I am. I guess I’m not supposed to do that? :joy:

There should be an optional third argument which is set to false by default which determines if you print the answers, if I’m remembering right…

1 Like

I’ve had a go at doing that, but now one of the errors has become a failure:

F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/boilerplate-arithmetic-formatter/test_module.py", line 14, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]')
AssertionError: 'Error: Too many problems.' != '  11      3801      1      123         1\[79 chars]----'
- Error: Too many problems.
+   11      3801      1      123         1
+  4    - 2999    + 2    +  49    - 9380
----    ------    ---    -----    ------
 : Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/boilerplate-arithmetic-formatter/test_module.py", line 39, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: '    32          1      45      123\n-  698  [94 chars] 172' != '   32         1      45      123\n- 698    -[86 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 arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.002s

FAILED (failures=2)
   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

Clearly, I’ve done it wrong…

you are returning the “too many problems” message instead of the arranged problems


and here the numbers are incorrectly spaced, lines with “+” are the expected ones, lines with “-” are yours

2 Likes

I’ve tried rearranging my code, but I’m still having this same problem… On the outside, it looks like every is lined up, but something is wrong on the inside.

Here’s my full code. Clearly, the second half of the script (from ‘if len(problems)==1’) is tediously long an inefficient, so that’s probably a factor:

def arithmetic_arranger(problems,v=None):
    n0=[]
    op=[]
    n1=[]
    da=[]
    an=[]
    if len(problems)>4:
        return "Error: Too many problems."
    else:
        for p in problems:
            sprob=p.split(' ')
            n0.append(sprob[0])
            n1.append(sprob[2])
            if len(sprob[0])>4 or len(sprob[2])>4:
                return "Error: Numbers cannot be more than four digits."
            if '+' in sprob[1]:
                try:
                    answer=int(sprob[0])+int(sprob[2])
                except:
                    return "Error: Numbers must only contain digits."
                an.append(str(answer))
                op.append('+')
            elif '-' in sprob[1]:
                try:
                    answer=int(sprob[0])-int(sprob[2])
                except:
                    return "Error: Numbers must only contain digits."
                an.append(str(answer))
                op.append('-')
            else:
                return "Error: Operator must be '+' or '-'."
            dash='-'*(len(str(sprob[0]))+len(str(sprob[2])))
            da.append(dash)

        jn0=' '.join(n0)
        jop=' '.join(op)
        jn1=' '.join(n1)
        jda=' '.join(da)
        jan=' '.join(an)
        sp4=' '*4

        if len(problems)==1:
            arranged_problems=\
            jn0.rjust(len(jda))+'\n'+\
            jop+jn1.rjust(len(jda)-1)+'\n'+\
            jda
            if v==True:
                arranged_problems+-'\n'+jan.rjust(len(jda))
        elif len(problems)==2:
            n0a,n0b=jn0.split(' ')
            opa,opb=jop.split(' ')
            n1a,n1b=jn1.split(' ')
            daa,dab=jda.split(' ')
            ana,anb=jan.split(' ')
            arranged_problems=\
            n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+'\n'+\
            opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+'\n'+\
            daa+sp4+dab
            if v==True:
                arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab)) 
        elif len(problems)==3:
            n0a,n0b,n0c=jn0.split(' ')
            opa,opb,opc=jop.split(' ')
            n1a,n1b,n1c=jn1.split(' ')
            daa,dab,dac=jda.split(' ')
            ana,anb,anc=jan.split(' ')
            arranged_problems=\
            n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+sp4+n0c.rjust(len(dac))+'\n'+\
            opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+sp4+opc+n1c.rjust(len(dac)-1)+'\n'+\
            daa+sp4+dab+sp4+dac
            if v==True:
                arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab))+sp4+anc.rjust(len(dac)) 
        elif len(problems)==4:
            n0a,n0b,n0c,n0d=jn0.split(' ')
            opa,opb,opc,opd=jop.split(' ')
            n1a,n1b,n1c,n1d=jn1.split(' ')
            daa,dab,dac,dad=jda.split(' ')
            ana,anb,anc,anD=jan.split(' ')
            arranged_problems=\
            n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+sp4+n0c.rjust(len(dac))+sp4+n0d.rjust(len(dad))+'\n'+\
            opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+sp4+opd+n1c.rjust(len(dac)-1)+sp4+opd+n1d.rjust(len(dad)-1)+'\n'+\
            daa+sp4+dab+sp4+dac+sp4+dad
            if v==True:
                arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab))+sp4+anc.rjust(len(dac))+sp4+anD.rjust(len(dad)) 
    return arranged_problems

And the errors:

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

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/boilerplate-arithmetic-formatter/test_module.py", line 39, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: '   32        1      45      123\n- 698    -3801    - 4[72 chars] 172' != '   32         1      45      123\n- 698    - 3801    +[76 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 arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.003s

FAILED (failures=2)
   32     3801      45      123
+ 698    -   2    - 43    -  49
-----    -----    ----    -----

Clearly you solved the ‘too many problems’ error and was too enthousiastic on correcting the spaces. Apperantly in the first 2 problems there is space missing and your dash line is 1 dash too short, so somewhere in that part something is not working as you want it to.
Would your output of the first two problems look like it is supposed to?

Furthermore, there seems to be an inconsistency in the elif (len(problems)==3 and elif (len(problems)==4 parts (lines 69 and 81). why changing +sp4 + opc + n1c in line 81?

(output from first error)

-    3     3801      45      123
+     3      3801      45      123
? +    +
- +855    -   2    + 43    +  49
+ + 855    -    2    + 43    +  49
?  +           +
- ----    -----    ----    -----
+ -----    ------    ----    ----- 

(output from second error)

-    3     3801      45      123
+     3      3801      45      123
? +    +
- +855    -   2    + 43    +  49
+ + 855    -    2    + 43    +  49
?  +           +
- ----    -----    ----    -----
+ -----    ------    ----    ----- 
2 Likes

Thank you all for your help! I’ve finally completed it!

As pointed out by Brain150, the inconsistency in line 81 was a type error.

The first problem was the script printed the ‘too many problems’ error when there were more than 4 problems, when it was supposed to be more than 5. My misunderstanding of the instructions.

Second, the amount of dashes were mathematically inconsistent when changing up the numbers. I needed it to line up with the bigger number, and not the answer. Because the answer may have more digits than the comprising numbers.

def arithmetic_arranger(problems,v=None):
    n0=[]
    op=[]
    n1=[]
    da=[]
    an=[]
    for p in problems:
        sprob=p.split(' ')
        n0.append(sprob[0])
        n1.append(sprob[2])
        if len(sprob[0])>4 or len(sprob[2])>4:
            return "Error: Numbers cannot be more than four digits."
        if '+' in sprob[1]:
            try:
                answer=int(sprob[0])+int(sprob[2])
            except:
                return "Error: Numbers must only contain digits."
            an.append(str(answer))
            op.append('+')
        elif '-' in sprob[1]:
            try:
                answer=int(sprob[0])-int(sprob[2])
            except:
                return "Error: Numbers must only contain digits."
            an.append(str(answer))
            op.append('-')
        else:
            return "Error: Operator must be '+' or '-'."
        if int(sprob[0])>int(sprob[2]):
            dash='-'*(len(sprob[0])+2)
            da.append(dash)
        elif int(sprob[0])<int(sprob[2]):
            dash='-'*(len(sprob[2])+2)
            da.append(dash)

    jn0=' '.join(n0)
    jop=' '.join(op)
    jn1=' '.join(n1)
    jda=' '.join(da)
    jan=' '.join(an)
    sp4=' '*4

    if len(problems)==1:
        arranged_problems=\
        jn0.rjust(len(jda))+'\n'+\
        jop+jn1.rjust(len(jda)-1)+'\n'+\
        jda
        if v==True:
            arranged_problems+-'\n'+jan.rjust(len(jda))
    elif len(problems)==2:
        n0a,n0b=jn0.split(' ')
        opa,opb=jop.split(' ')
        n1a,n1b=jn1.split(' ')
        daa,dab=jda.split(' ')
        ana,anb=jan.split(' ')
        arranged_problems=\
        n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+'\n'+\
        opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+'\n'+\
        daa+sp4+dab
        if v==True:
            arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab)) 
    elif len(problems)==3:
        n0a,n0b,n0c=jn0.split(' ')
        opa,opb,opc=jop.split(' ')
        n1a,n1b,n1c=jn1.split(' ')
        daa,dab,dac=jda.split(' ')
        ana,anb,anc=jan.split(' ')
        arranged_problems=\
        n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+sp4+n0c.rjust(len(dac))+'\n'+\
        opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+sp4+opc+n1c.rjust(len(dac)-1)+'\n'+\
        daa+sp4+dab+sp4+dac
        if v==True:
            arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab))+sp4+anc.rjust(len(dac)) 
    elif len(problems)==4:
        n0a,n0b,n0c,n0d=jn0.split(' ')
        opa,opb,opc,opd=jop.split(' ')
        n1a,n1b,n1c,n1d=jn1.split(' ')
        daa,dab,dac,dad=jda.split(' ')
        ana,anb,anc,anD=jan.split(' ')
        arranged_problems=\
        n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+sp4+n0c.rjust(len(dac))+sp4+n0d.rjust(len(dad))+'\n'+\
        opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+sp4+opc+n1c.rjust(len(dac)-1)+sp4+opd+n1d.rjust(len(dad)-1)+'\n'+\
        daa+sp4+dab+sp4+dac+sp4+dad
        if v==True:
            arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab))+sp4+anc.rjust(len(dac))+sp4+anD.rjust(len(dad))
    elif len(problems)==5:
        n0a,n0b,n0c,n0d,n0e=jn0.split(' ')
        opa,opb,opc,opd,ope=jop.split(' ')
        n1a,n1b,n1c,n1d,n1e=jn1.split(' ')
        daa,dab,dac,dad,dae=jda.split(' ')
        ana,anb,anc,anD,ane=jan.split(' ')
        arranged_problems=\
        n0a.rjust(len(daa))+sp4+n0b.rjust(len(dab))+sp4+n0c.rjust(len(dac))+sp4+n0d.rjust(len(dad))+sp4+n0e.rjust(len(dae))+'\n'+\
        opa+n1a.rjust(len(daa)-1)+sp4+opb+n1b.rjust(len(dab)-1)+sp4+opc+n1c.rjust(len(dac)-1)+sp4+opd+n1d.rjust(len(dad)-1)+sp4+ope+n1e.rjust(len(dae)-1)+'\n'+\
        daa+sp4+dab+sp4+dac+sp4+dad+sp4+dae
        if v==True:
            arranged_problems+='\n'+ana.rjust(len(daa))+sp4+anb.rjust(len(dab))+sp4+anc.rjust(len(dac))+sp4+anD.rjust(len(dad))+sp4+ane.rjust(len(dae))
    else:
        return "Error: Too many problems."
    return arranged_problems
1 Like

Inconsistency is a… uhm…very nasty thing!
Nice work rereading and finding the errors in your code

1 Like

It certainly doesn’t help with having a mountain of code like I had. :joy:

I’ve learnt a lot with this first assignment - especially the need to mix up the tested parameters. On to the next one!!