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
Your code so far*
def arithmetic_arranger(problems, show_answers=False):
arranged_problems = “”
first_line = “”
second_line = “”
dashes = “”
answers = “”

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

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

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

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

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

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

#if show_answers:
    if parts[1] == "+":
        answer = str(int(parts[0]) + int(parts[2]))
    else:
        answer = str(int(parts[0]) - int(parts[2]))
    answers += answer.rjust(max_length) + "    "

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

if show_answers:
    arranged_problems += "\n" + answers.rstrip()

return arranged_problems

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

Your browser information:

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

Link to the challenge:

Can you show your output or link to a replit?

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[1]  not in ["+", "-"]:
        return "Error: Operator must be '+' or '-'."
    
    if not segment[0].isdigit() and not segment[2].isdigit():
        return "Error: Numbers must only contain digits."
    
    if len(segment[0]) > 4 or len(segment[2]) > 4:
        return "Error: Numbers cannot be more than four digits."
    
    
gap = max(len(segment[0]), len(segment[2])) + 2
first_line += segment[0].rjust(gap) + "    "
second_line += segment[1]  + " " + segment[2].rjust(gap-2) + "    "
third_line += '-' * gap + "    "

#if show_solutions:

if segment[1] == "+":
        solution = str(int(segment[0]) + int(segment[2]))
else:
        solution = str(int(segment[0]) - int(segment[2]))
    
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[0]), len(segment[2])) + 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

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