# Arithmetic formatter spacing problem

ive been having trouble fixing this since I have no idea why this happens. Basically when I use the four spaces to separate the problems, it goes all crazy. When I used the exact same code on pycharm (and adding a print function just before the return statement, both having the same argument/result) it works perfectly. I tried to use ‘\t’ instead of the spaces and even though it looks better, the problems still dont align perfectly

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

#too many problems solution
if len(problems) > 5:

return 'Error: Too many problems.'

#variables for the output/return results
first = ''
second = ''
dash = ''

#Main for loop to get each problem seperately
for problem in problems:
#for loop to solve the correct operators error
for characters in problem:
characters.replace(' ', '')
if '/' in characters or '*' in characters:
return "Error: Operator must be '+' or '-'."

#variables to get each individual item of the problem
problemx = problem.split()
problemx1 = problemx[0]
problemxo = problemx[1]
problemx2 = problemx[2]

#solution to the max digits Error
if len(problemx1) >= 5 or len(problemx2) >= 5:
return 'Error: Numbers cannot be more than four digits.'

#if statement for the answer variable and for the only digits Error solution
if problemxo == '+':
try:
add = int(problemx1) + int(problemx2)
answer += str(add).rjust(len(max(problem)) + 2) + "    "
except:
return 'Error: Numbers must only contain digits.'
elif problemxo == '-':
try:
sub = int(problemx1) - int(problemx2)
answer += str(sub).rjust(len(max(problem)) + 2) + "    "
except:
return 'Error: Numbers must only contain digits.'

#lenght variable for correct formatting.
if len(problemx1) > len(problemx2):
lenght = len(problemx1)
elif len(problemx2) > len(problemx1):
lenght = len(problemx2)
elif len(problemx2) == len(problemx1):
lenght = len(problemx1)

#building up the variables for the output
first += problemx1.rjust(lenght + 2) + '    '
second += problemxo + problemx2.rjust(lenght + 1) + '    '
dash += '-' * (lenght + 2) + '    '

#if statement to output the results with answers or no answers
return first + "\n" + second + "\n" + dash + "\n" + answer
else:
return first + "\n" + second + "\n" + dash + "\n"
``````

And this is the error

``````FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-5/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   [37 chars]  \n' != '    [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"]
``````

In the error message this is the first problem I see.

Yeah but what does that mean exactly? How can I fix it?

The error text zooms in to the earliest part of the output that contains the problem. If there are a number of characters that cannot be displayed either before or after the issue, the number of characters that get truncated from the string get counted and placed in brackets. You need to go through and compare your output (the line to the left of the !=) and the expected output (the right side). Anywhere where the spaces don’t line up exactly (click and drag your mouse to count them, if you can’t see the difference with your eyes) needs to be corrected before it will accept your answer.

``````AssertionError: '    [23 chars]  123    \n+ 855    -    2    + 43    +  49   [37 chars]  \n' != '    [23 chars]  123\n+ 855    -    2    + 43    +  49\n-----[23 chars]----'
``````

So if you look at the section eoja pointed out, you can see the problem:

``````       Your ouput:  [23 chars]  123    \n
``````
``````  Expected output:  [23 chars]  123\n
``````
1 Like

i have fixed part of it. All that’s left is to get the two spaces between the dash and the ‘[21 chars]’ and the space between the “\n” and the “+”. The first one I have no idea how to solve it and the second one everytime I try to solve it it changes completly.

Here is the error

``````FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-5/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: '    [25 chars]123\n + 855    -    2    + 43    +  49\n------[21 chars]----' != '    [25 chars]123\n+ 855    -    2    + 43    +  49\n-----  [21 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"]
``````

Here is the code that I’ve changed.

``````#lenght variable for correct formatting.
if len(problemx1) > len(problemx2):
lenght = len(problemx1)
elif len(problemx2) > len(problemx1):
lenght = len(problemx2)
elif len(problemx2) == len(problemx1):
lenght = len(problemx1)

#building up the variables for the output
first += problemx1.rjust(lenght + 2) + '    '
second +=" "+ problemxo +  problemx2.rjust(lenght + 1) + '   '
dash += '-' * (lenght + 2) + '   '

#if statement to output the results with answers or no answers
return first.rstrip() + "\n" + second.rstrip() + "\n" + '--' + dash.rstrip() + answer
else:
return first.rstrip() + "\n" + second.rstrip() + "\n" + '--' + dash.rstrip()

``````

This is the code that changes the expected output

``````first += problemx1.rjust(lenght + 2) + '    '
second += problemxo +  problemx2.rjust(lenght + 1) + '   '
dash += '-' * (lenght + 2) + '   '

#if statement to output the results with answers or no answers
return first.rstrip() + "\n" + second.rstrip() + "\n" + '--' + dash.rstrip() + answer
else:
return first.rstrip() + "\n" + second.rstrip() + "\n" + '--' + dash.rstrip()
``````

And this is the code with the changed expected output

``````FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-5/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: '    [33 chars]55   -    2   + 43   +  49\n-------   ------   ----   -----' != '    [33 chars]55    -    2    + 43    +  49\n-----    ------[13 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"]`````````

Try this: rather than posting the entire line, copy your output and lay it over the expected output and then go through and check for differences. Like this:

``````'    [33 chars]55   -    2   + 43   +  49\n-------   ------   ----   -----'
'    [33 chars]55    -    2    + 43    +  49\n-----    ------[13 chars]----'
``````

Personally I prefer developing my code in a text editor and then pasting it into the replit. One thing that helped me get the spacing right for this project was trying different inputs once I had it looking good and seeing how it changed. Once you think your have it all lined up, try using numbers of different lengths and then seeing if it changes things. If it does, you have a logic error beyond just the testing suite being picky about white space.

I finally made it, I just had to put the items of each variable inside an f string and with that formatting it was wayyy easier. Thanks a lot guys I probably wouldnt have figured it out without y’alls help!

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