# Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Describe your issue in detail here.
I have written the code for the arithemetic formatter but when I run it only the last item is displayed
arranged_problems = “”
first_line = “”
second_line = “”
dashes = “”

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

for problem in problems:
parts = problem.split()

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

if not parts.isdigit() or not parts.isdigit():
return "Error: Numbers must only contain digits."

if len(parts) > 4 or len(parts) > 4:
return "Error: Numbers cannot be more than four digits."

max_length = max(len(parts), len(parts)) + 2
first_line += parts.rjust(max_length) + "    "
second_line += parts + " " + parts.rjust(max_length - 2) + "    "
dashes += "-" * max_length + "    "

if parts == "+":
else:

arranged_problems += first_line.rstrip() + "\n"
arranged_problems += second_line.rstrip() + "\n"
arranged_problems += dashes.rstrip()

return arranged_problems
``````

print(arithmetic_arranger([“32 + 8”, “1 - 3801”, “9999 + 9999”, “523 - 49”], True))

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36`

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

I tested this code and it worked ok for me. Output:

``````  32         1      9999      523
+  8    - 3801    + 9999    -  49
----    ------    ------    -----
40     -3800     19998      474
``````

I’m sorry. I actually copied and pasted the wrong code here. The initial post contains a code given to me by a friend and it actually works. Mine looks similar but only prints the last item.
I’m posting mine below:
def arithmetic_arranger(problems, show_solutions=False):

``````arranged_problems = ""
first_line = ""
second_line = ""
third_line = ""
fourth_line = ""

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

for problem in problems:
segment = problem.split()

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

if not segment.isdigit() and not segment.isdigit():
return "Error: Numbers must only contain digits."

if len(segment) > 4 or len(segment) > 4:
return "Error: Numbers cannot be more than four digits."

gap = max(len(segment), len(segment)) + 2
first_line += segment.rjust(gap) + "    "
second_line += segment  + " " + segment.rjust(gap-2) + "    "
third_line += '-' * gap + "    "
``````

#if show_solutions:

``````if segment == "+":
solution = str(int(segment) + int(segment))
else:
solution = str(int(segment) - int(segment))

fourth_line += solution.rjust(gap) + "    "

#arranged_problems = first_line.rstrip() + "\n" + second_line.rstrip() + "\n" + third_line.rstrip() + "\n" + fourth_line.rstrip()

arranged_problems += first_line + "\n"
arranged_problems += second_line.rstrip() + "\n"
arranged_problems += third_line.rstrip()

if show_solutions:
arranged_problems += "\n" + fourth_line.rstrip()

return arranged_problems
``````

print(arithmetic_arranger([“32 + 8”, “1 - 3801”, “9999 + 9999”, “523 - 49”], True))

Print out the `segment` variable right before the line:

``````gap = max(len(segment), len(segment)) + 2
``````

You have a loop/indentation prob. You loop through all the segments for the checks, but then you lose the indentation and only work on the last prob.

I’ve checked my indentation several times. I can’t see the problem. please elaborate more

Thanks @pkdvalis, I’ve traced the problem. Sooooo excited.
Thanks a lot