# Help with Arithmetic Formatter please

Hi, I am still a beginner so I tried to create the code in the most comprehensive way I could, but I still can not understand what is the problem with the spaces in the result. I would really appreciate it if someone could help me.

My code:

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

#initialize variables
numbers1 = []
oper = []
numbers2 = []

if len(problems) > 5:
return "Error: Too many problems."

#loops trough values and Checks for errors
for i in problems :
problem = i.split()

numbers1.append(problem)

if problem != "+" and problem != "-":
return "Error: Operator must be '+' or '-'."

oper.append(problem)

numbers2.append(problem)

#Checks if the input are only numbers
if not (problem.isdigit() and problem.isdigit()):
return "Error: Numbers must only contain digits."

#Checks lenght of given numbers
if len(problem) > 4 or len(problem) > 4:
return "Error: Numbers cannot be more than four digits."

arranged_problems = ""

#initialize rows
topRow = ""
bottomRow = ""
separator = ""
resultsFormat = ""

#Figures out the amount of spaces in top and bottom row by the lenght of the numbers
for i in range(len(problems)):
if len(numbers1[i]) > len(numbers2[i]):
totalLen = len(numbers1[i]) + 2

else:
totalLen = len(numbers2[i]) + 2

if i != 0 :

topRow += "    "
bottomRow += "    "
separator += "    "

#writes top row
topRow += " " * (totalLen - len(numbers1[i]))
topRow += numbers1[i]

#writes bottom row
bottomRow += oper[i] + ( " " * (totalLen - len(numbers2[i]) - 1))
bottomRow += numbers2[i]

#writes separator
separator += "-" * totalLen

arranged_problems = "{}\n{}\n{}" .format(topRow, bottomRow, separator)

#Calculates the results
if status == True :

for i in range(len(problems)):

if oper[i] == "+":
result = int(numbers1[i]) + int(numbers2[i])

elif oper[i] == "-":
result = int(numbers1[i]) - int(numbers2[i])

if i != 0 :
resultsFormat += "    "

resultsFormat +=  " " * (totalLen - len(str(result)))
resultsFormat += str(result)

arranged_problems +=  "\n{}" .format(resultsFormat)

return arranged_problems
``````

Error message:

``````   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
...F..
======================================================================
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 arithmetic problems and a second argument of `True`.')
AssertionError: '   3[68 chars]-    ------    ----    -----\n -666    -3800       88      172' != '   3[68 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.006s

FAILED (failures=1)
``````

Look at the difference between the two sides of the Assertion Error:

``````AssertionError:
'   3[68 chars]-    ------    ----    -----\n -666    -3800       88      172' !=
'   3[68 chars]-    ------    ----    -----\n -666     -3800      88      172'
``````

your `3800` is one space to the left

I get that, what I do not understand is why that is happening

somewhere in here where you calculate the spaces, you need to check carefully that everything works as expected

1 Like

I managed to use a better way to formate the result string, I still do not understand what was creating that problem, but I finished the challenge anyway.

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