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.
Your code so far
def arithmetic_arranger(problems,display_answ = False):
if len(problems) > 5:
return “Error: Too many problems.”
problems_list = problems
try:
display_answers = display_answ
except:
display_answers = False
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
#Answers
answers_line = ""
if display_answers == True:
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):
answers_line += " "
answers_line += result
answers_line += four_spaces
counter += 1
arranged_problems = (first_line + "\n" + second_line + "\n" + separation_line + "\n" + answers_line)
return arranged_problems
Your browser information:
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
Link to the challenge: