Hello,
I am building the Arithmetic Formatter Project as a part of Scientific Computing with Python certificate.
Although my code is working properly on VS Code and passing all the test scenarios, I still don’t know why it doesn’t pass the same tests on the FreeCodeCamp Platform.
Here’s my code if that can help :
def arithmetic_arranger(problems, show_answers=False):
operator = ""
elements = []
top_line = []
bottom_line = []
list_separator = []
result_list = []
if len(problems) <= 5:
i = 0
for problem in problems:
if problem.find("+") != -1:
operator = "+"
bottom_line.append(operator)
elements = problem.split(' + ')
if elements[0].isdigit() and elements[1].isdigit():
result = int(elements[0]) + int(elements[1])
else:
return f'Error: Numbers must only contain digits.'
elif problem.find("-") != -1:
operator = "-"
bottom_line.append(operator)
elements = problem.split(' - ')
if elements[0].isdigit() and elements[1].isdigit():
result = int(elements[0]) - int(elements[1])
else:
return f'Error: Numbers must only contain digits.'
else:
return f'Error: Operator must be \'+\' or \'-\'.'
max_digits = max(len(elements[0]), len(elements[1]))
min_digits = min(len(elements[0]), len(elements[1]))
if max_digits <= 4:
if len(elements[1]) == min_digits:
top_line.append(''.join([' '*2, elements[0],'\t']))
bottom_line.append(''.join([' '*abs(max_digits - min_digits + 1), elements[1],'\t']))
else:
top_line.append(''.join([' '*abs(max_digits - min_digits + 2), elements[0],'\t']))
bottom_line.append(''.join([' ', elements[1],'\t']))
list_separator.append(''.join(['-'*(max_digits + 2), '\t']))
result_list.append(''.join([' '*(max_digits + 2 - len(str(result))), str(result),'\t']))
top_string = ''.join(top_line)
bottom_string = ''.join(bottom_line)
separator_string = ''.join(list_separator)
result_string = ''.join(result_list)
i += 1
if i == len(problems):
if show_answers is True:
return f"""{top_string}\n{bottom_string}\n{separator_string}\n{result_string}"""
else:
return f"""{top_string}\n{bottom_string}\n{separator_string}"""
else:
return f'Error: Numbers cannot be more than four digits.'
else:
return f'Error: Too many problems.'
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
THANKS IN ADVANCE !!!
