Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My function is working, however, it is not passing the tests

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    data = [[], [], []]
    operator = []
    solution = []
    if len(problems) >= 5:
        print('Error: Too many problems.')
        if operator == '+':
            if show_answers == True:
                solution = int(first) + int(second)
        if operator == '-':
            if show_answers == True:
                solution = int(first) - int(second)
        else:
            return "Error: Operator must be '+' or '-'."
        if len(elements[0]) > 4 or len(elements[2]) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        if not elements[0].isdigit() or not elements[2].isdigit():
            return 'Error: Numbers must only contain digits.'
    for problem in problems:
        elements = problem.split()
        if len(elements[2]) >= len(elements[0]):
            second = f'{elements[1]} {elements[2]}'
        else:
            second = f'{elements[1]}{" " * (len(elements[0])- 1)}{elements[2]}'
        
        data[0].append(' '*(len(second) - len(elements[0])) + elements[0])
        data[1].append(second)
        data[2].append('-' * len(second))
    solution.append('    '.join(data[0]))
    solution.append('    '.join(data[1]))
    solution.append('    '.join(data[2]))
    return '\n'.join(solution)

print(arithmetic_arranger(problems=["1 + 2", "1 - 9380"], show_answers=True))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Hi there and welcome to our community!

Using the repr function, you can more easily see where your function output differs from the output expected by the tests.

For instance, your code is failing the first test, so try adding this below your function:

# function output
print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))
# expected output
print(repr('  3801      123\n-    2    +  49\n------    -----'))

You’ll see that the formatting doesn’t exactly match up with the requirements.

More about repr():