Arithmetic Formatter Test Error

The “Scientific Computing with Python” certification project “Arithmetic Formatter” is causing me some trouble, specifically in repl.it. After testing the code and ensuring it meets all the project reqs, I moved my code to repl.it and am getting these errors:

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/fcc-arithmetic-arranger-2/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: None != '    3      3801      45      123\n+ 855 [56 chars]----' : 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/fcc-arithmetic-arranger-2/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: None != '   32         1      45      123\n- 698 [90 chars] 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.002s

FAILED (failures=2)

My code is as follows:

def arithmetic_arranger(problems, display=False):
    
    operators = []
    longest_operands = []
    len_diffs = []

    num_spaces = []

    if len(problems) > 5:
        return "Error: Too many problems."
    
    for problem in range(len(problems)):
        if "+" in problems[problem]:
            problems[problem] = problems[problem].split(" + ")
            operators.append("+")
            
        elif "-" in problems[problem]:
            problems[problem] = problems[problem].split(" - ")
            operators.append("-")

        else:
            return "Error: Operator must be '+' or '-'."

    for problem in range(len(problems)):
        if problems[problem][0].isdigit() == False:
            return "Error: Numbers must only contain digits."
        elif problems[problem][1].isdigit() == False:
            return "Error: Numbers must only contain digits."

    for problem in range(len(problems)):
        if len(problems[problem][0]) > 4:
            return "Error: Numbers cannot be more than four digits."
        elif len(problems[problem][1]) > 4:
            return "Error: Numbers cannot be more than four digits."

    for problem in range(len(problems)):
        if len(problems[problem][0]) < len(problems[problem][1]):
            longest_operands.append("1")

            len_diffs.append(1 + (len(problems[problem][1])-len(problems[problem][0])))
        else:
            longest_operands.append("-1")

            len_diffs.append(-1 - (len(problems[problem][0])-len(problems[problem][1])))

    #---------------------------------

    for lens in range(len(len_diffs)):
        if len_diffs[lens] > 0:
            for num_times_print in range(len_diffs[lens]):
                print(" ", end='')
            print(" ",end='')
            print(problems[lens][0], end='')

            if lens != 3:
                print("    ", end='')
        else:
            print("  ", end='')
            print(problems[lens][0], end='')

            if lens != 3:
                print("    ", end='')

    print("")

    for lens in range(len(len_diffs)):
        print(operators[lens], end='')
        if len_diffs[lens] < 0:
            for spaces in range(abs(len_diffs[lens])):
                print(" ", end='')
            print(problems[lens][1], end='')

            if lens != 3:
                print("    ", end='')
        else:
            print(" ", end='')
            print(problems[lens][1], end='')

            if lens != 3:
                print("    ", end='')

    print("")

    for operands in range(len(longest_operands)):
        if longest_operands[operands] == "-1":
            for number_of_dashes in range(len(problems[operands][0])+2):
                print("-", end='')
        else:
            for number_of_dashes in range(len(problems[operands][1])+2):
                print("-", end='')
                

        if operands != 3:
            print("    ", end='')

    print("")
    
    if display == True:
        solutions = []
        for problem in range(len(problems)):
            if operators[problem] == "+":
                solutions.append(int(problems[problem][0]) + int(problems[problem][1]))
            else:
                solutions.append(int(problems[problem][0]) - int(problems[problem][1]))

        for problem in range(len(problems)):
            if len(problems[problem][0]) > len(problems[problem][1]):
                num_spaces.append(len(str(problems[problem][0])) - len(str(solutions[problem])))
            else:
                num_spaces.append(len(str(problems[problem][1])) - len(str(solutions[problem])))

        for spaces in range(len(num_spaces)):
            for i in range(2 + int(num_spaces[spaces])):
                print(" ", end='')
                
            print(solutions[spaces], end='')

            if spaces != 3:
                print("    ", end='')

And here is the output when running the test case (on my IDE) that shows up in the errors:

image

Please advise on further steps and troubleshooting, TIA. Keep in mind that I have read the forums and concluded that this is not a problem with whitespace and newlines. If you need some code explanation, please feel free to ask.

Browser Information
Microsoft Edge Version 84.0.522.63 (Official build) (64-bit)

you need a function that return values
your function doesn’t have a return statement for when you do not have errors

1 Like

@ilenia Thanks; but it passes 4/6 of the tests with the code I have described above (without the returns). Is it necessary to change the prints to returns to pass the other tests?

you return the errors, but not return the formatted numbers

so you don’t pass those tests that do not expect an error

2 Likes

Thank you. That reduces it to one failure instead of 2. The new failure is as follows:

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/fcc-arithmetic-arranger-4/test_module.py", line 14, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]')
AssertionError: '  11[26 chars]     1    \n+  4    - 2999    + 2    +  49- 93[44 chars]    ' != '  11[26 chars]         1\n+  4    - 2999    + 2    +  49    [44 chars]----'
-   11      3801      1      123     1    
?                                     ----
+   11      3801      1      123         1
?                                    ++++
- +  4    - 2999    + 2    +  49- 9380    
?                                     ----
+ +  4    - 2999    + 2    +  49    - 9380
?                               ++++
- ----    ------    ---    -----------    ?                                ---------
+ ----    ------    ---    -----    ------?                      +++++++++
 : Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]

----------------------------------------------------------------------
Ran 6 tests in 0.008s

FAILED (failures=1)

After observation, I noted that the spaces between the \n and one of the numbers were different. How do I fix this, as it works just fine on my IDE?

have you checked if you have the spaces also in your IDE?

you should not have spaces before the new line characters

1 Like