Arithmetic Formatter - Help

Tell us what’s happening:

Code is showing error again and again

Your code so far

def arithmetic_arranger(problems,x=True):
  if x:
    top_final = ''
    middles = ''
    operations = ''
    if len(problems) > 5:
      return 'Error: Too many problems.'
    for w in problems:
      s = (w.split(' '))
      up = s[0]
      y = s[1]
      d = s[2]
      if len(up) > 4 or len(d) > 4:
        return 'Error: Numbers cannot be more than four digits.'
      if not up.isnumeric() or not d.isnumeric():
        return 'Error: Numbers must only contain digits.'
      if  y == '+' or y == '-':
        length = max(len(up), len(d)) + 2
        top = str(up).rjust(length)
        bottom = y + str(d).rjust(length - 1)
        sum = ''
        for s in range(length):
          sum += '-'
        top_final += top + '    '
        middles += bottom + '    '
        operations += sum + '    '   
      else: 
        return "Error: Operator must be '+' or '-'."
    top_final.rstrip()
    middles.rstrip()
    operations.rstrip()

    arranged_problems = top_final + '\n' + middles + '\n' + operations
    return arranged_problems

ERROR-

   32      3801      45      123    
+ 698    -    2    + 43    +  49    
-----    ------    ----    -----    
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/VirtualFixedElements/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/VirtualFixedElements/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 arithemetic problems and a second argument of `True`.')
AssertionError: '   3[23 chars]  123    \n- 698    - 3801    + 43    +  49   [35 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 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.005s

FAILED (failures=2)

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

You might want to reformat your post for readability, especially the code sections.

Hi and welcome to the forum.

It looks like the trailing whitespace differs from what is expected.

A post was split to a new topic: Arithmetic formatter review