Arithmetic Formatter Project

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 !!!

Hi @bourhan.hleiss and welcome to our community!

I’ve edited your post to format your code correctly and also to include a direct link to the challenge to make it easier for people to assist. In future, it’s easiest just to click on the Help button, which appears after you have submitted incorrect code three times. This will create a form help post which automatically includes your full code, a direct link to the challenge and an opportunity for you to describe your issue in detail before you submit the post to the forum.

You have issues with your spacing. Your output needs to exactly match the required output. Inside your print commands, you can wrap the contents in repr() to show exactly what your function is returning and compare it with the expected output.

Without using repr() however, you can still see that the formatting doesn’t quite match:

NOTE: The expected output contains only newline and individual space characters (no tabs or other spacing characters), for formatting purposes.

EXAMPLE:

   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028