Code is working if I manually Test it but doesnt pass the test module

Tell us what’s happening:

Describe your issue in detail here.
I have the code working so that when I manually call the function and compare the output to the expected output they appear to be the same.
For some reason however the code is not passing the test_module and produces a failure for the test_arrangement and the test_solutions. For both if I compare the print of the expected string and the returned string they look identical to me.

Your code so far

def arithmetic_arranger(problems,SecondValue = False):
  line1 = ""
  line2 = ""
  line3 = ""
  line4 = ""
  Allthelines = ""
  i = 0
  if len(problems) > 5 :
     return "Error: Too many problems."
  for n in problems:
    n= n.split()
    if n[1] != "+" and n[1] != "-":
      return "Error: Operator must be '+' or '-'."
    if len(n[0])>4 or len(n[2])>4: 
      return "Error: Numbers cannot be more than four digits."
    if not str.isdigit(n[0]) or not str.isdigit(n[2]): 
      return "Error: Numbers must only contain digits."
    if int(n[0])>=int(n[2]):
      abbbb = str(int(n[0]) + int(n[2]))      
    else: 
      abbbb = "-" + str(int(n[2])-int(n[0]))
    n[0]= " " + n[0]
    n[2]= " " + n[2]
    while len(n[0])>len(n[2]) :
      n[2]= " " + n[2]
    while len(n[0])<len(n[2]) :
      n[0]= " " + n[0]
    while len(n[0])> len(abbbb):
      abbbb = " " + abbbb
    nline3 ="-"
    for d in n[0]: 
      nline3 = nline3 + "-"

    nline1 = " " + n[0] 
    nline2 = n[1] + n[2] 
    nline4 = " " + abbbb    

    if i< len(problems):
      nline1 = nline1 + "    "
      nline2 = nline2 + "    "
      nline3 = nline3 + "    "
      nline4 = nline4 + "    "
      i += 1

    line1 = line1 + nline1 
    line2 = line2 + nline2 
    line3 = line3 + nline3    
    line4 = line4 + nline4 

  Allthelines = line1 + "\n" + line2 + "\n" + line3 
  if SecondValue: 
    Allthelines = Allthelines + "\n" + line4
    
  return Allthelinestype or paste code here

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.73

Challenge: Arithmetic Formatter

Link to the challenge:

It looks like you are putting too many spaces at the end of the lines. But seeing the test suite output would be very helpful.

Thanks for the quick reply here the console log:
I actually accounted for that and I am only adding the 4 spaces to the string if it is not the last one in the line.

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/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: '    [23 chars]  123    \n+ 855    -    2    + 43    +  49   [35 chars]    ' != '    [23 chars]  123\n+ 855    -    2    + 43    +  49\n-----[23 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 "/home/runner/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: '   3[23 chars]  123    \n- 698    - 3801    + 43    +  49   [73 chars]    ' != '   3[23 chars]  123\n- 698    - 3801    + 43    +  49\n-----[57 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.004s

FAILED (failures=2)

I think you have an ‘off by one’ in your counting though.

Found it thank you very much =)

If you trim to just the diff, the forum will color highlight the output:

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

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