# Aritmetic Formatter Project

This is the code that I wrote for Arithmetic Formatter Project but 2 tests were failed and I couldn’t figure out what’s wrong with it. Could you just help me ?
Here’s the the code:

``````def arithmetic_arranger(problems,default=None):
if len(problems) > 5:
arranged_problems="Error: Too many problems."
return arranged_problems

operations = []

def compare (a,b):
if len(a) > len(b):
return len(a)
else:
return len(b)

c = 0

for i in problems:
argfinder = i.find(" ")
a1 = i[0:argfinder]
a2 = i[argfinder+3:]
if not a1.isnumeric() or not a2.isnumeric():
arranged_problems = "Error: Numbers must only contain digits."
return arranged_problems
if len(a1) > 4 or len(a2) > 4:
arranged_problems = "Error: Numbers cannot be more than four digits."
return arranged_problems
op = i[argfinder+1]
if op != "+" and op != "-":
arranged_problems = "Error: Operator must be '+' or '-'."
return arranged_problems
calc = 0
if op == "+":
calc = int(a1) + int(a2)
elif op == "-":
calc = int(a1) - int(a2)
out = str(calc)
dash = "-"*(compare(a1,a2)+2)
space_out = " "*abs(len(dash) - len(str(out))) + out
operations.append([])

if len(a1) <= len(a2):
space_a1 = " "*(abs(len(a1)-len(a2))+2) + a1
op_space_a2 = op + " " + a2
operations[c].append(space_a1)
operations[c].append(op_space_a2)
operations[c].append(dash)

else:
space_a1 = " "*(abs(len(dash)-compare(a1,a2))) + a1
op_space_a2 = op+(" "*abs(len(a1)-len(a2)+1))+a2
operations[c].append(space_a1)
operations[c].append(op_space_a2)
operations[c].append(dash)

if default == True:
operations[c].append(space_out)

c += 1
arranged_problems = ""
for arr in zip(*operations):
arranged_problems += ('    '.join(arr))
arranged_problems += "\n"

return arranged_problems
``````

Here’s the test results:

``````python main.py
32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/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: '    [36 chars]   -    2    + 43    +  49\n-----    ------    ----    -----\n' != '    [36 chars]   -    2    + 43    +  49\n-----    ------    ----    -----'
3      3801      45      123
+ 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-1/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 arithmetic problems and a second argument of `True`.')
AssertionError: '   3[70 chars]   ------    ----    -----\n -666     -3800      88      172\n' != '   3[70 chars]   ------    ----    -----\n -666     -3800      88      172'
32         1      45      123
- 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`.

----------------------------------------------------------------------
Ran 6 tests in 0.002s

FAILED (failures=2)
``````

You might have an extra return `/n` at the end?

I used the arranged_problems.r.strip("\n") and arranged_problems.r.strip() after for loop I got the same results

I mean, this right here says there is an extra \n at the end that there shouldn’t be, so I suspect you aren’t stripping correctly. Or its the other way and you don’t have a `\n` that you should. Actual is on the left and expected is on the right.

It looks good on test I don’t get it. How can I do that ?

I don’t understand what you are asking. The output of the test suite says you added an extra `\n` at the end that should not be there. It must be removed (or never added in the first place).

Thanks mate I realized that I was stripping out but didn’t assigned to arranged_problems after that
I was using just this line :

arranged_problems.rstrip("\n")

But I should do that like this:

arranged_problems = arranged_problems.rstrip("\n")

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.