Hello everyone, I have been working on my arithmetic arranger project for days I don’t know where I got it wrong that cannot pass unit-test, I checked all the condition in my IDE it works fine but fail designated test please help;
the code:
def arithmetic_arranger(problems, display_result=False):
upper_value = ""
lower_value = ""
dashes = ""
f_result = ""
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
problem = problem.split()
x = problem[0]
y = problem[2]
operator = problem[1]
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
if len(x) > 4 or len(y) > 4:
return "Error: Numbers cannot be more than four digits."
try:
num_1 = int(x)
num_2 = int(y)
except:
return "Error: Numbers must only contain digits."
if operator == '+':
result = str(num_1 + num_2)
else:
result = str(num_1 - num_2)
space = max(len(x), len(y)) + 2
first = x.rjust(space)
second= str(operator + y.rjust(space - 1))
dash = str('-' * len(result)).rjust(space)
result = result.rjust(space)
if problem == problems[-1]:
upper_value += first
lower_value += second
dashes += dash
f_result += result
else:
upper_value += first + ' '
lower_value += second + ' '
dashes += dash + ' '
f_result += result + ' '
if display_result:
arranger = upper_value + '\n' + lower_value + '\n' + dashes + '\n' + f_result
else:
arranger = upper_value + '\n' + lower_value + '\n' + dashes
return arranger
console response it too long please check the link below
----------------------------------------------------------------------
Ran 6 tests in 0.039s
FAILED (failures=2)
^C
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: