Arithmetic Formatter Solution Printing

NFI what is going wrong here, all feedback welcome :slight_smile:

def arithmetic_arranger(problems, compute=False):
    
    line1 = ""
    line2 = ""
    line3 = ""
    line4 = ""
    
    if len(problems) > 5:
        return 'Error: Too many problems.'
    
    for i in problems:
        first, ops, second = i.split()
        if not first.isnumeric() or not second.isnumeric():
            return 'Error: Numbers must only contain digits.'
        elif len(first) > 4 or len(second) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        elif (ops == '+' or ops == '-'):
            if ops == "+":
                total = int(first) + int(second)
            else:
                total = int(first) - int(second)
            width = max(len(first), len(second)) + 2
            l1 = str(first).rjust(width)
            l2 = ops + str(second).rjust(width -1)
            l3 = "-" * width
            l4 = str(total).rjust(width)
            
            if i == problems[:-1]:
                line1 += l1
                line2 += l2
                line3 += l3
                line4 += l4
            else:
                line1 += l1 + '    '
                line2 += l2 + '    '
                line3 += l3 + '    '
                line4 += l4 + '    '
        else:
            return "Error: Operator must be '+' or '-'."
    
    if compute:
        arranged_problems = line1 + '\n' + line2 + '\n' + line3 + '\n' + line4
    else:
        arranged_problems = line1 + '\n' + line2 + '\n' + line3
    
    return arranged_problems

This is the output for the unit test

   32      3801      45      123    
+ 698    -    2    + 43    +  49    
-----    ------    ----    -----    
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/ForsakenPrettyComputergame/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/ForsakenPrettyComputergame/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   [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 arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.009s

FAILED (failures=2)
KeyboardInterrupt

Problem

Your current code is adding 4 extra spaces at the end of each line. Try it by editing the following lines to print a different character instead of a space:

...
else:
    line1 += l1 + 'xxxx'
    line2 += l2 + 'xxxx'
    line3 += l3 + 'xxxx'
    line4 += l4 + 'xxxx'
...

This will produce an output as below:

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

Cause

The cause of the extra spaces is this if statement:

if i == problems[:-1]:

which never turns True because it is slicing the problems list up to the second last element and comparing that to the current element i being processed.

Solution

Rewrite the above if statement as:

if i == problems[-1]: # Remove the semicolon

The if block will be evaluated hence no extra spaces at the end of each line.

Youโ€™re a peach, really appreciate the feedback :slight_smile: