# Stuck on Arithmetic Formatter is the test module wrong?

It gives me an error when I run the main module but my output matches that of expected output , it is properly arranged aswell.

``````def arithmetic_arranger(problems, answer=False):

Count = 0

for i in problems:
Count += 1
if Count > 5:
return "Error: Too many problems."

Loop = 0

for problem in problems:

Operands = problem.split(" ")
operator = Operands

if operator not in ['+','-']:
return "Error: Operator must be \'+\' or \'-\'."

a = Operands
b = Operands

try:
x = int(a)
y = int(b)
except ValueError:
return "Error: Numbers must only contain digits."

n = len(a)
m = len(b)

if n > 4:
return "Error: Numbers cannot be more than four digits."
elif m > 4:
return "Error: Numbers cannot be more than four digits."

if n > m:
First_Line = ((n+2-n)*' ') + a
Second_Line = operator + ' ' + ((n-m)*' ') + b
Third_Line = ((n+2)*'-')

elif n < m:
First_Line = "  " + ((m-n)*' ') + a
Second_Line = operator + ((m-n)*' ') + b
Third_Line = ((m+2)*'-')

elif n == m:
First_Line = "  " + a
Second_Line = operator + " " + b
Third_Line = ((n+2)*'-')

if operator == '+':
Fourth_Line = x + y
Fourth_Line = str(Fourth_Line)
space = len(Third_Line) - len(str(Fourth_Line))
Fourth_Line = (space* ' ') + Fourth_Line
elif operator == '-':
Fourth_Line = x - y
Fourth_Line = str(Fourth_Line)
space = len(Third_Line) - len(Fourth_Line)
Fourth_Line = (space* ' ') + Fourth_Line

if Loop == 0:
p = First_Line
q = Second_Line
r = Third_Line
s = Fourth_Line

elif Loop != 0:
p += "    " + First_Line
q += "    " + Second_Line
r += "    " + Third_Line
s += "    " + Fourth_Line
if Loop == 0:
p = First_Line
q = Second_Line
r = Third_Line

elif Loop != 0:
p += "    " + First_Line
q += "    " + Second_Line
r += "    " + Third_Line

Loop += 1

arranged = ( p + '\n' + q + '\n' + r + '\n' + s + '\n')
arranged = ( p + '\n' + q + '\n' + r + '\n')

return arranged
print(arithmetic_arranger(["32 + 679", "3800 - 2", "45 + 43", "123 + 4900", "770 - 9"], False))
``````

can you post your repl or the tests output?

sure here you go

``````python main.py
32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
730      3799      88      172

F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/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: '    [27 chars]3\n+  855    -    2    + 43    +  49\n-----   [22 chars]--\n' != '    [27 chars]3\n+ 855    -    2    + 43    +  49\n-----    [19 chars]----'
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/boilerplate-arithmetic-formatter/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[36 chars]   -   3801    + 43    +  49\n-----    ------ [48 chars]72\n' != '   3[36 chars]   - 3801    + 43    +  49\n-----    ------   [44 chars] 172'
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.003s

FAILED (failures=2)
exit status 1
``````

The `-` line is your output, the `+` line is the expected output and the `?` line indicates what is wrong with the `-`. In this case, it is trying to tell you that you have extra whitespace at the end that the test was not expecting.

1 Like

oh, I see…
How can I make it error free in that case…
Thanks you need to not add the extra character at the end

but i did not add any. see my code above i simply made lines that contain right aligned problems and added 4 spaces between 2 diff problems. and after the last problem there are no extra spaces. In fact i guess the expected has an extra space

yeah, it’s not an extra space, but it’s still an extra character - the string should end with a number or a dash depending on the second function argument

1 Like

solving that would be a great step, then you have other stuff to fix, and these are extra spaces

1 Like

I solved it. I just removed the \n from the end of both of these variables and test gave an ok.