Tell us what’s happening:
the output seems to be correct but doesn’t pass the test
Your code so far
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return 'Error: Too many problems.'
one = ''
two = '\n'
three = '\n'
four = '\n'
for problem in problems:
prob = problem.split()
if prob[1] == '*' or prob[1] == '/':
return "Error: Operator must be '+' or '-'."
if len(prob[0]) > 4 or len(prob[2]) > 4:
return 'Error: Numbers cannot be more than four digits.'
if not prob[0].isdigit() or not prob[2].isdigit():
return 'Error: Numbers must only contain digits.'
n1 = prob[0]
op = prob[1]
n2 = prob[2]
answer = 0
n1_len = len(prob[0])
n2_len = len(prob[2])
if op == '+':
answer = int(n1) + int(n2)
else:
answer = int(n1) - int(n2)
ans_len = len(str(answer))
dash_len = 2 + max(n1_len, n2_len)
n1_gap = (dash_len - n1_len) * ' '
op_alignment = max(n1_len, n2_len) + 1
n2_gap = (op_alignment - n2_len) * ' '
total_space = len(n2_gap) + 1 + n2_len
op_gap = (dash_len - total_space) * ' '
ans_gap = (dash_len - ans_len) * ' '
gap=' '*4
one += f"{n1_gap}{n1}{gap}"
two += f"{op_gap}{op}{n2_gap}{n2}{gap}"
three += f"{dash_len*'-'}{gap}"
four += f"{ans_gap}{answer}{gap}"
if show_answers:
result = one + two + three + four
else:
result = one + two + three
return result
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project