# Arithmetic Formatter TEST Problems

Problem
I wrote the code and I tested on PyCharm and the outputs are the same as the required, but when I try it in repl.it all the test FAIL and I don’t know why.

def arithmetic_arranger(problems,display_answ = False):
if len(problems) > 5:
return “Error: Too many problems.”

``````problems_list = problems
try:
except:
first_operands = list()
second_operands = list()
operators = list()
spacing = list()
for problem in problems_list:
data = problem.split()
first_operand = data[0]
second_operand = data[2]
operator = data[1]

valid_operators = ('+','-')
if operator not in valid_operators:
return "Error: Operator must be '+' or '-'."

try:
first_operand = int(first_operand)
second_operand = int(second_operand)
except:
return "Error: Numbers must only contain digits."

if first_operand > 9999 or second_operand > 9999:
return "Error: Numbers cannot be digits more than four."

first_operands.append(str(first_operand))
second_operands.append(str(second_operand))
operators.append(operator)

#Spacing
max_length = 1
if first_operand > 9 or second_operand > 9:
max_length = 2
if first_operand > 99 or second_operand > 99:
max_length = 3
if first_operand > 999 or second_operand > 999:
max_length = 4
spacing.append(max_length)

four_spaces = "    "
#Print first operands
first_line = ""
counter = 0
for number in first_operands:

spaces = (spacing[counter] + 2) - len(number)
for space in range(spaces):
first_line += " "
first_line = first_line + number + four_spaces
counter += 1

#Print Operators and second operands
second_line = ""
counter = 0
for number in second_operands:
spaces = (spacing[counter] + 2) - len(number) - 1
second_line += operators[counter]
for space in range(spaces):
second_line += " "
second_line = second_line + number + four_spaces
counter += 1

#Print separator
separation_line = ""
counter = 0
for operation in problems_list:
for line in range(spacing[counter] + 2):
separation_line += "-"
separation_line += four_spaces
counter += 1

number_of_spaces = separation_line.split()
counter = 0
result = 0
for operation in problems_list:
if operators[counter] == '+':
result = str(int(first_operands[counter]) + int(second_operands[counter]))
if operators[counter] == '-':
result = str(int(first_operands[counter]) - int(second_operands[counter]))
spaces = len(number_of_spaces[counter]) - len(result)
for space in range(spaces):
counter += 1
arranged_problems = (first_line + "\n" + second_line + "\n" + separation_line + "\n" + answers_line)
return arranged_problems
``````

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

Challenge: Arithmetic Formatter

Hi @nil73 ,

1. There seems to be extra spaces at the end of each line, try stripping those off.
2. Check the error message displayed on operands being more than 4 digits. It needs to be exactly the one specified in the instruction, else test fails.
3. Cases where answers need not be displayed, there should not be ‘\n’ at the end of the separation line.

The output looks exactly the way it’s required but these extra spaces or different error messages could fail the tests.

Hope this helps.

1 Like

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