Arthmetic Formatter

Hey guys.
I’m having a weird error with my arithmetic formatter. The first and the last tests are failing. But when I try the code using Visual Studio Code, the output seems to be perfectly fine. I’ve compared the output with what is expected in the test module file.
Attaching my entire code along with the stated errors:

def arithmetic_arranger(problems, solve = False):
  fline = ""
  sline = ""
  lines = ""
  sumx = ""
  string = ""
  if len(problems)>5:
    return "Error: Too many problems."
  for problem in range(len(problems)):
    problems[problem]=problems[problem].split()
    num1 = problems[problem][0]
    num2 = problems[problem][2]
    operator = problems[problem][1]
    if len(num1)>4 or len(num2)>4: 
        return "Error: Numbers cannot be more than four digits."
    try: 
        num1int=int(num1)
        num2int=int(num2)
    except ValueError: 
        return "Error: Numbers must only contain digits." 
    sum = ""
    if operator=='+':
        sum=str(num1int + num2int)
    elif operator=='-':
      sum=str(num1int - num2int)
    else:
      return "Error: Operator must be '+' or '-'."
    length = max(len(num1), len(num2)) + 2 
    top = str(num1).rjust(length)
    bottom = operator + str(num2).rjust(length - 1)
    res = str(sum).rjust(length)
    line = ""
    for i in range (length):
      line += "-"


    if problem != problems[-1]:
      fline += top + '    '
      sline += bottom + '    '
      lines += line + '    '
      sumx += res + '    '
    else: 
      fline += top 
      fline += bottom 
      lines += line 
      sumx += res 
  string = fline + "\n" + sline + "\n" + lines
  if solve: 
    string = string + "\n" + sumx + "\n"
  return string 
  

The following error is thrown:
 python main.py

   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/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   [75 chars]  \n' != '   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.005s

FAILED (failures=2)

Any help would be highly appreciated. Thanks in advance!

In the first line “-” is your code, “?” marks differences and “+” is the expected code.

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

Thank you so much! Finally realised that the issue was the trailing whitespace.
Used simple .rstrip() functions to solve the issue.

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