Arithmetic Formatter: What's the Error?

Tell us what’s happening:
When I disable test_module.py, I get the following output:

   32      3801      45      123    
+ 698    -    2    + 43    +  49    
-----    ------    ----    -----

If I then add True to the print statement, I get the following:

   32      3801      45      123    
+ 698    -    2    + 43    +  49    
-----    ------    ----    -----    
  730      3799      88      172    

Looks right to me, but when I enable the test module, I get:

   32      3801      45      123    
+ 698    -    2    + 43    +  49    
-----    ------    ----    -----    
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-5/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-5/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)

Your code so far

def arithmetic_arranger(problems, solve = False):
  arranged_problems = ""
  firsts, operators, seconds, widths, answers = [], [], [], [], []
  if len(problems) > 5:
    return "Error: Too many problems."
  # Tokenize strings
  for p in problems:
    # Operator check
    if p.find(" + ") != -1:
      part = p.partition(" + ")
    elif p.find(" - ") != -1:
      part = p.partition(" - ")
    else:
      return "Error: Operator must be '+' or '-'."
    # Operand syntax check
    if (part[0].isdigit() and part[2].isdigit()) == False:
      return "Error: Numbers must only contain digits."
    # Operand size check
    if len(part[0]) > 4 or len(part[2]) > 4: return "Error: Numbers cannot be more than four digits."
    #Pass it out of the loop
    firsts.append(part[0])
    operators.append(part[1].strip())
    seconds.append(part[2])
    widths.append(max(len(part[0]), len(part[2])) + 2)
    if part[1] == " + ":
      answers.append(str(int(part[0]) + int(part[2])))
    elif part[1] == " - ":
      answers.append(str(int(part[0]) - int(part[2])))
  # Fill top row
  for i in range(len(problems)):
    arranged_problems += firsts[i].rjust(widths[i], ' ')
    if i < len(problems): arranged_problems += "    "
  arranged_problems += '\n'
  #Fill middle row
  for i in range(len(problems)):
    arranged_problems += operators[i] + ' ' + seconds[i].rjust(widths[i] - 2, ' ')
    if i < len(problems): arranged_problems += "    "
  arranged_problems += '\n'
  #Fill bottom row
  for i in range(len(problems)):
    for j in range(widths[i]):
      arranged_problems += '-'
    if i < len(problems): arranged_problems += "    "
  if solve:
    arranged_problems += '\n'
    for i in range(len(problems)):
      arranged_problems += answers[i].rjust(widths[i], ' ')
      if i < len(problems): arranged_problems += "    "
  return arranged_problems

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0

Challenge: Arithmetic Formatter

Link to the challenge:

Next time copy the full error into the code-format AND I added the first 3 lines to show what it does :wink:
Long story short, you got extra spaces at the end of your lines.

- [your output]
? [differences]
+ [expected output]
-     3      3801      45      123    
?                                 ----
+     3      3801      45      123
- + 855    -    2    + 43    +  49    
?                                 ----
+ + 855    -    2    + 43    +  49
- -----    ------    ----    -----    
?                                 ----
+ -----    ------    ----    -----
1 Like

I thought I did copy the full error, but thanks for the feedback nonetheless.