# Arithmetic Formatter - Help!

Hi all,

I have been working on the arithmetic arranger challenge for the scientific computing with python course and I am struggling with passing the tests on Replit.

Everything is working great in my Spyder IDE on my laptop, but when I go to run my code in Replit I can’t pass the “arrangement” tests. However, on Spyder it looks and works exactly as expected. Can someone please help me figure out where I’m going wrong? I know this code could be improved, but I’m a total newbie and this is what made sense to me.

Code so far:

def arithmetic_arranger(problems, display = False):

``````import re

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

line1 = []
line2 = []
line3 = []
ans_line = []
width_com = []

for prob in problems:
first_blank = prob.find(" ")
first_num = prob[:first_blank]
sec_line = prob[first_blank+1:]
line1.append(first_num)
line2.append(sec_line)
second_blank = prob.rfind(" ")
second_num = prob[second_blank+1:]

if len(first_num) > 4 or len(second_num) > 4:
return "Error: Numbers cannot be more than four digits."
operator = prob[second_blank-1]
width = max(len(first_num), len(second_num)) + 2
width_com.append(width)
dashes = "-" * width
line3.append(dashes)

if operator == "+":
try:
answer = int(first_num) + int(second_num)
except:
return "Error: Numbers must only contain digits."
elif operator == "-":
try:
answer = int(first_num) - int(second_num)
except:
return "Error: Numbers must only contain digits."
else:
return "Error: Operator must be '+' or '-'."

#print(line1, line2, line3, ans_line, width_com)

this_one = 0
line_1_fin = str()
line_2_fin = str()
line_3_fin = str()
ans_line_fin = str()

for num1 in line1:
line_1_fin += num1.rjust(width_com[this_one]) + "    "
#print(width_com[this_one])
this_one += 1

this_one = 0

for num2 in line2:
punc = re.findall('[+-]', num2)
line_2_fin += punc[0]
num2_fin = re.findall('[0-9]+', num2)
#print(num2_fin)
line_2_fin += num2_fin[0].rjust(width_com[this_one]-1) + "    "
this_one += 1

#print(punc)

this_one = 0

for num3 in line3:
line_3_fin += num3.rjust(width_com[this_one]) + "    "
this_one += 1

this_one = 0

for num4 in ans_line:
ans_line_fin += num4.rjust(width_com[this_one]) + "    "
this_one += 1

if display == False:
return print(line_1_fin.rstrip(), line_2_fin.rstrip(), line_3_fin.rstrip(), sep="\n")
if display == True:
return print(line_1_fin.rstrip(), line_2_fin.rstrip(), line_3_fin.rstrip(), ans_line_fin.rstrip(), sep="\n")
``````

arithmetic_arranger( [“3801 - 2”, “123 + 49”])

Hi everyone,

I figured out my error. It was with my return line at the very end. What I should have done is declared a variable before the 4 “Display” lines like the below and then use return “final_format”.

final_format = line_1_fin.rstrip() + “\n” + line_2_fin.rstrip() + “\n” + line_3_fin.rstrip()

I finally passed the first challenge!

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