Arithmetic Arranger Errors

Hi! I need a little help. I keep getting errors on this project, but I don’t understand what’s the issue… or issues. Here is my code so far:

def arithmetic_arranger(problems, default=None):
    if len(problems) >= 5:
        return "Error: Too many problems."
    else:
        line1 = ""
        line2 = ""
        dots = ""
        res = ""
        for i in problems:
            p = i.split(" ")
            num1 = p[0]
            op = p[1]
            num2 = p[2]

            if num1.isalnum() or num2.isalnum():
                return "Error: Numbers must only contain digits."
            if len(num1) > 4 or len(num2) > 4:
                return "Error: Numbers cannot be more than four digits."
            if op == "+":
                ans = int(num1) + int(num2)
            elif op == "-":
                ans = int(num1) - int(num2)
            else:
                return "Error: Operator must be '+' or '-'."
            w = max(len(num1), len(num2))
            line1 += num1.rjust(w+2)
            line2 += op + num2.rjust(w + 1)
            dots += "-" * (w + 2)
            res += str(ans).rjust(w + 2)
        if default:
            arranged_problems = line1 + "\n" + line2 + "\n" + dots + "\n" + res
        else:
            arranged_problems = line1 + "\n" + line2 + "\n" + dots

    return arranged_problems

Thanks!

Look at the errors (yours may look different since I’m using an old version of the project locally):

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gray/src/work/fcc-sc-arithmetic-arranger/test_module.py", line 16, in test_arrangement
    self.assertEqual(
AssertionError: 'Error: Numbers must only contain digits.' != '    3      3801      45      123\n+ 855  [55 chars]----'
- Error: Numbers must only contain digits.
+     3      3801      45      123
+ 855    -    2    + 43    +  49
-----    ------    ----    -----
 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

This is flagging the input, which is only digits, as containing non-digits. The problem must be here:

Check the documentation for .isalnum().

Second, there is a spacing issue:

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gray/src/work/fcc-sc-arithmetic-arranger/test_module.py", line 94, in test_solutions
    self.assertEqual(
AssertionError: '   32     1  45  123\n- 698- 3801+ 43+  49\n-------[31 chars] 172' != '   32         1      45      123\n- 698    - 3801  [79 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`.

Notice how your output (the - lines) are missing space compared to the expected (the + lines) since the required amount of space is missing.

1 Like