Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I have completed the project and camparing with the true, there are no mistakes in my output, but I can’t pass the test

Your code so far

def display(first_num, second_num, operator, larger_digit):
    transf_num = []
    i = 0
    for number in first_num:
        space = larger_digit[i] - len(number)
        while space > 0:
            transf_num.append(' ')
            space -= 1
        transf_num.append(number)
        i += 1
        if i >= len(first_num):
            transf_num.append('\n')
        else:
            transf_num.append('    ')
    formatted_num = ''.join(transf_num)
    transf_num = []
    i = 0
    for number in second_num:
        transf_num.append(operator[i] + ' ')
        space = larger_digit[i] - len(number) - 2
        while space > 0:
            transf_num.append(' ')
            space -= 1
        transf_num.append(number)
        i += 1
        if i >= len(second_num):
            transf_num.append('\n')
        else:
            transf_num.append('    ')
    formatted_num += (''.join(transf_num))
    transf_num = []
    while i > 0:
        index = len(first_num) - i
        while larger_digit[index] > 0:
            transf_num.append('-')
            larger_digit[index] -= 1
        if i > 0:
            transf_num.append('    ')            
        i -= 1
    formatted_num += (''.join(transf_num))
    return formatted_num

def arithmetic_arranger(problems, show_answers=False):   
    if len(problems) > 5:
        problems = 'Error: Too many problems.'
        return problems

    first_num = []
    second_num = []
    station = []
    operator = []
    larger_digit = []
    index = 0

    for problem in problems:
        brf_pro = problem.replace(' ','')
        digit_num = 0
        num_order = 1       
        for char in brf_pro:
            if char.isdigit():
                digit_num += 1
                if digit_num > 4:
                    problems = 'Error: Numbers cannot be more than four digits.'
                    return problems
                else:
                    station.append(char)
            elif  char == '*' or char == '/':
                problems = "Error: Operator must be '+' or '-'."
                return problems
            elif char == '+' or char == '-':
                larger_digit.append(digit_num)
                digit_num = 0
                operator.append(char)
                first_num.append(''.join(station))
                station = []
            else:
                problems = 'Error: Numbers must only contain digits.'
                return problems

        second_num.append(''.join(station))
        station = []

        if larger_digit[index] < digit_num:
            larger_digit[index] = digit_num + 2
        else:
            larger_digit[index] += 2

        index += 1

    larger_digit1 = []
    for num in larger_digit:
        larger_digit1.append(num)
    
    problems = display(first_num, second_num, operator, larger_digit)

    if show_answers:
        answers = []
        i = 0
        while i < len(first_num):
            if operator[i] == '+':
                mid_answer = int(first_num[i]) + int(second_num[i])
            else:
                mid_answer = int(first_num[i]) - int(second_num[i])
            answers.append(mid_answer)
            i += 1
        transf_num = []
        i = 0
        for number in answers:
            str_num = str(number)
            space = larger_digit1[i] - len(str_num)
            while space > 0:
                transf_num.append(' ')
                space -= 1
            transf_num.append(str_num)
            i += 1
            if i < len(first_num):
                transf_num.append('    ')
        problems += '\n' + ''.join(transf_num)
                         
    return problems

print(f'{arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])}')
print('    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

if the test doesn’t pass your code is not correct

you may want to follow the advice of opening the browser console with F12 to see a more detailed output of the tests

Click here to show how to read the errors in the console

The assertion error and diff gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

This is called a diff, and it shows you the differences between two files or blocks of code:

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!

an other thing you can do is use repr(), like print(repr(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])))
this will print a string like this:

'    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----    '

so you can compare it with the one shown in the tests.
I will put them one after the other to show you for this one:

actual:   '    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----    '
expected: '    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----'

there is a difference between the two