# Arithmetic Formatter - Did not pass test in replit

I have done my code for the Arithmetic Formatter exercise and failed to pass the test in replit. However, i tested the case in my own python IDLE local host and manage to pass all of those.

Is there anything that I’m doing wrong?
Below is the code for the exercise.

def arithmetic_arranger(problems, display = False):
line_one = list()
line_two = list()
line_three = list()
line_four = list()

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

for problem in problems:
lst = problem.split()
first_number = lst[0]
operator = lst[1]
second_number = lst[2]

try:
int(first_number)
int(second_number)
except:
print('Error: Numbers must only contain digits.')
return

if len(first_number) > 4 or len(second_number) > 4:
print('Error: Numbers cannot be more than four digits.')
return

if operator == '+' or operator == '-':
length = max(len(first_number), len(second_number)) + 2

line_one.append(first_number.rjust(length))
line_two.append(operator + second_number.rjust(length-1))
line_three.append('-'*length)

if operator == '+':
solution = int(first_number) + int(second_number)
else:
solution = int(first_number) - int(second_number)

line_four.append(str(solution).rjust(length))
else:
print('Error: Operator must be \'+\' or \'-\'.')
return

one = '    '.join(line_one)
two = '    '.join(line_two)
three = '    '.join(line_three)
four = '    '.join(line_four)

print(one)
print(two)
print(three)
if display == True:
print(four)``````

I see a couple of problems straight off.
1- You are not returning one string, you are printing to the console.
2- You are adding spaces to the begining of each line.

Thanks for your feedback, jpottercycles. I don’t get what you meant by Adding spaces to the beginning of each line. Could you kindly please explain further?

each line starts with ’ '.

I’ve done some changes to the code and tested it again with replit and managed to pass all the test. Thanks for your feedback, i realized the problem lies with my understanding of return and print. Now i understand the difference usage between them.

Below is the code after making necessary changes. (I replaced print to return mostly)

def arithmetic_arranger(problems, display = False):
line_one = list()
line_two = list()
line_three = list()
line_four = list()

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

for problem in problems:
lst = problem.split()
first_number = lst[0]
operator = lst[1]
second_number = lst[2]

try:
int(first_number)
int(second_number)
except:
return 'Error: Numbers must only contain digits.'

if len(first_number) > 4 or len(second_number) > 4:
return 'Error: Numbers cannot be more than four digits.'

if operator == '+' or operator == '-':
length = max(len(first_number), len(second_number)) + 2

line_one.append(first_number.rjust(length))
line_two.append(operator + second_number.rjust(length-1))
line_three.append('-'*length)

if operator == '+':
solution = int(first_number) + int(second_number)
else:
solution = int(first_number) - int(second_number)

line_four.append(str(solution).rjust(length))
else:
return 'Error: Operator must be \'+\' or \'-\'.'

one = '    '.join(line_one)
two = '    '.join(line_two)
three = '    '.join(line_three)
four = '    '.join(line_four)

if display == True:
print(one+'\n'+two+'\n'+three+'\n'+four)
return one+'\n'+two+'\n'+three+'\n'+four
else:
print(one+'\n'+two+'\n'+three)
return one+'\n'+two+'\n'+three``````

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