# Arithmetic Formatter Python Help

Tell us what’s happening:
Describe your issue in detail here.
I have done all the things that the challenge is asking and the only thing left is the 4 spaces between each problem which I am not able to figure out how to do. In my current code, I have used rjust() function to align it which I think is why there is the problem because it does not take in account the number of digits there are (in my code that is). I have also added some spaces at the end just so it looks closer to the correct result, but those spaces are not practical. So I would request you to help me in figuring out how to keep those 4 spaces between each problem.

``````import re
def arithmetic_arranger(problems, showAns = False):
ans = ""
result = ""
arr = []
leng = []
sign = []
if len(problems) > 5:
return "Error: Too many problems."
# Extracting the numbers
for i in problems:
if len(re.findall("[A-Z,a-z]+", i)) > 0:
return "Error: Numbers must only contain digits."
arr = arr + re.findall("[0-9]+", i)
# Printing top row
for i in range(0, len(arr), 2):
if len(arr[i]) > 4:
return "Error: Numbers cannot be more than four digits."
result = result + arr[i].rjust(6) + "   "
result = result.rstrip()
result = result + "\n"
# Extracting the signs
for i in problems:
sign = sign + re.findall("[+,-]", i)
if len(sign) != len(problems):
return "Error: Operator must be '+' or '-'."
# Preparing middle row with signs
for i in range(1, len(arr), 2):
if len(arr[i]) > 4:
return "Error: Numbers cannot be more than four digits."
# Keeping appropriate signs
for j in range(len(sign)):
if i != (j + j + 1):
continue
else:
s = sign[j]
# Answers to questions if showAns is true
if showAns:
if sign[j] == "+":
sd = int(arr[i]) + int(arr[i - 1])
ans = ans + (str(sd)).rjust(6) + "   "
elif sign[j] == "-":
sd = int(arr[i - 1]) - int(arr[i])
ans = ans + (str(sd)).rjust(6) + "   "
# Length of spaces between signs and the middle row number
if len(arr[i - 1]) - len(arr[i]) <= 0:
spaces = 1
else:
spaces = (len(arr[i - 1]) - len(arr[i])) + 1
sp = ""
while spaces != 0:
sp = sp + " "
spaces = spaces - 1
l = s + sp + arr[i]
leng.append(l)
# Printing the middle row
result = result + l.rjust(6) + "   "
result = result.rstrip()
result = result + "\n"
# Printing the bottom row with appropraite number of dashes (-)
for i in leng:
d = len(i)
dash = ""
while d != 0:
dash = dash + "-"
d = d - 1
result = result + dash.rjust(6) + "   "
result = result.rstrip()
# Outputting result
if not showAns:
return result
else:
result = result + "\n"
result = result + ans
return result
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; rv:88.0) Gecko/20100101 Firefox/88.0`

Challenge: Arithmetic Formatter

You are not too far from solving this!
If you think that the problem lies in the rjust() alignment not taking the number of digits into account, can you set it up so that it aligns and takes the longest argument ( of the first line, second line, dash line or solution line) into account? This should solve your alignment issues.
Same with the 4 spaces: it should be 4 spaces between the longest argument and the operator of the next problem.

Take a look at your results below ( `-` is your result, `+` is the desired outcome and `? ` are the differences).
It seems there is 1 space too many in front of the first problem and 1 too many in fronf of the 3rd problem.
Then, at the dash line 1 space too many before the first dashes and 1 space short before the 2nd group of dashes and 1 too many before the 3rd group (and extra spaces at the end, which you already thought are not practical ;))

``````-      3     3801       45      123
? -                    -
+     3      3801      45      123
?      +
-  + 855   -    2     + 43    +  49
? -                  -
+ + 855    -    2    + 43    +  49
?         +
-  -----   ------     ----    -----? -                  -
+ -----    ------    ----    -----?      +
``````
``````-     32        1       45      123
? -              -
+    32         1      45      123
?              +
-  - 698   - 3801     + 43    +  49
? -                  -
+ - 698    - 3801    + 43    +  49
?         +
-  -----   ------     ----    -----
? -                  -
+ -----    ------    ----    -----
?      +
-   -666    -3800       88      172   ? -                    -           ---
+  -666     -3800      88      172?      +
``````

Can you work with this?

Ok I used your suggestion to make the rjust() function take the longest argument from the first and second line and add it by 2, and later I added 4 spaces at the last and it worked. At the beginning it was not working because the max() function works weirdly with strings (apparently according to Python, string “49” > string “146”), but later I resolved it and the function is working perfectly fine.

Below is the code of what I did in the rjust() function

``````align = max([int(arr[i]), int(arr[i + 1])])
result = result + arr[i].rjust(len(str(align)) + 2) + "    "
``````

The `+` in `int(arr[i + 1])` can be changed into `-` as per the need and the code does what it needs to.