Python - Arithmetic Formatter Problem

I’m not sure what’s wrong here. I’ve written code to handle the key rules for this challenge and all the tests look to be showing the correctly formatted answers, but they are not evaluating in the tests. My code is below:

def arithmetic_arranger(problems, torf=True):
  # if optional argument is true, execute code
  if torf:
    #define variables used later
    nums = ''
    dens = ''
    sums = ''
    # error when too many problems
    if len(problems) > 5:
      return 'Error: Too many problems.'
    # split problem into components
    for k in problems:
      l = (k.split(' '))
      num = l[0]
      op = l[1]
      den = l[2]
      # error when numbers are too long or non-numeric
      if len(num) > 4 or len(den) > 4:
        return 'Error: Numbers cannot be more than four digits.'
      if not num.isnumeric() or not den.isnumeric():
        return 'Error: Numbers must only contain digits.'
      # execute code if operators are correct
      if  op == '+' or op == '-':
        # set length of sum and top, bottom and line values
        length = max(len(num), len(den)) + 2
        top = str(num).rjust(length)
        bottom = op + str(den).rjust(length - 1)
        sum = ''
        for s in range(length):
          sum += '-'
        # add to the overall string
        nums += top + '    '
        dens += bottom + '    '
        sums += sum + '    '   
      else: 
        return "Error: Operator must be '+' or '-'."
    # strip out spaces to the right of the string
    nums.rstrip()
    dens.rstrip()
    sums.rstrip()

    arranged_problems = nums + '\n' + dens + '\n' + sums
    return arranged_problems

Below is the error message shown in the console.

    3      3801      45      123    
+ 855    -    2    + 43    +  49    
-----    ------    ----    -----    
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/GratefulCuddlyDatasets/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/GratefulCuddlyDatasets/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)

Link to my repl.it here

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

you need to just use the given numbers, and display them as required, not do the calculations

Oof, is one not allowed to use format strings in this challenge? This looks tedious as all get-out.

I’m not trying to solve the calculations - just trying to format the given numbers in the correct way. It looks like they are showing formatted correctly but the FCC test is not evaluating my code as being correct. That’s the help I was asking for as I’m not sure what’s wrong with my solution.

I’m not sure. I also tried a workaround where the code would add the spaces to display the numbers in the correct places, but that showed the same errors too.

you have spaces before the \n character after the last number on a line, instead there should not be

I’m not able to tell you if there’s an error or where that would be - looks like I’m much more of a beginner than you - just wanted to say hi and thank you for posting your code as it’s helping me figure out where I was stuck!

1 Like

Instead of stripping the empty strings like that, you can use join method with empty spaces.

nums = []
for top in tops:
    nums.append(top)

"    ".join(nums)

Implement this logic within your code.

Updated:
I saw you rstrip the end spaces but you didn’t take the return value.

Try

nums = nums.rstrip()

Also you don’t have the answer line implemented. So the test with answer will not pass