Arithmetic Formatter: Code working as intended but still failing two cases

Tell us what’s happening:
The code is working fine for 4 out of 6 cases, but is failing for the remaining 2 cases. The problem is that it seems to be working just fine on my PC and is returning Output as intended. I am unable to figure out what’s wrong with it.

Your code so far

def arithmetic_arranger(problems, solve = False):
  # TOO Many Problems Error
  if len(problems) > 5:
    return "Error: Too many problems."
  
  first = '' # The First Operand 
  second = ''# The Operator and the Second Operand
  dash = ''# The dashline
  solution = ''# The Solution
  
  for problem in problems:
    problem = problem.split() # String into List Conversion

    # Check for Unsupported Operand
    if problem[1] == '+' or problem[1] == '-':
      pass
    else:
      return "Error: Operator must be '+' or '-'." 
    
    # Check for Operand Size
    if len(problem[0]) > 4 or len(problem[2]) > 4:
      return "Error: Numbers cannot be more than four digits."
    
    # Everything Else
    first += problem[0].rjust(len(max(problem)) + 2) + "\t" 
    second += problem[1] + problem[2].rjust(len(max(problem)) + 1) + "\t"
    dash += '-' * (len(max(problem)) + 2) + "\t"
    
    # The Solutions and the Last Error Check
    try:
      
      if problem[1] == "+":
        add = int(problem[0]) + int(problem[2])
        solution += str(add).rjust(len(max(problem)) + 2) + "\t"
      
      else:
        sub = int(problem[0]) - int(problem[2])
        solution += str(sub).rjust(len(max(problem)) + 2) + "\t"
    
    except:
      return 'Error: Numbers must only contain digits.'
      
  if solve == True:
    return first + "\n" + second + "\n" + dash + "\n" + solution
  else:
    return first + "\n" + second + "\n" + dash + "\n"

Output:

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: '    3\t  3801\t  45\t 123\t\n+ 855\t-    2\t+ [37 chars]\t\n' != '    3      3801      45      123\n+ 855    -  [50 chars]----'
-     3   3801    45     123
- + 855 -    2  + 43    + 49
- ----- ------  ----    ----
+     3      3801      45      123
+ + 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: '   32\t     1\t  45\t 123\t\n- 698\t- 3801\t+ [64 chars]72\t' != '   32         1      45      123\n- 698    - 3[84 chars] 172'
-    32      1    45     123
- - 698 - 3801  + 43    + 49
- ----- ------  ----    ----
-  -666  -3800    88     172    +    32         1      45      123
+ - 698    - 3801    + 43    +  49
+ -----    ------    ----    -----
+  -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`.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

the lines starting with - are your output and the lines starting with + are the expected ones, your spacing is not correct

1 Like

Thanks! :smile:
Took me quite a while to get the right amount of whitespaces at all the right places :sweat_smile:

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