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

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 + " "