Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I think my code is doing what it’s supposed to, based on the instructions. But none of the tests are succeeding. The console shows that the function should return a string with the ‘\n’ character showing up. Do I need to make that character actually visible?

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    final_string_1 = ' '
    final_string_2 = ' '
    final_string_3 = ' '
    final_string_4 = ' '
    operator_str = ''
    max_number_length = []
    le_grand_number_str = ''
    le_grand_final_string = ''
    alphabet_error = False
    for i in problems:
        top_number = i.split(' ', 1)[0]
        le_grand_number_str += top_number
        operator = i.split(' ', 2)[1]
        operator_str += operator
        bottom_number = i.split(' ', 2)[2]
        le_grand_number_str += bottom_number
        if not le_grand_number_str.isnumeric():
            alphabet_error = True
            break
        a = len(top_number)
        b = len(bottom_number)
        max_number_length.append(max(a, b))
        c = a - b
        if c >= 0:
            final_string_1 += ' ' * 2 + top_number
        elif c < 0:
            final_string_1 += ' ' * (2 + (c * -1)) + top_number
        final_string_1 += ' ' * 4
        if i == len(problems):
            final_string_1 += ' '
        final_string_2 += operator
        if c >= 0:
            final_string_2 += ' ' * (c + 1) + bottom_number
        elif c < 0:
            final_string_2 += ' ' + bottom_number
        final_string_2 += ' ' * 4
        if i == len(problems):
            final_string_2 += ' '
        final_string_3 += '-' * (max(a, b) + 2)
        final_string_3 += ' ' * 4
        if i == len(problems):
            final_string_3 += ' '
        evaluated_number = str(eval(i))
        d = len(evaluated_number)
        final_string_4 += ' ' * (max(a, b) + 2 - d)
        final_string_4 += evaluated_number
        final_string_4 += ' ' * 4
        if i == len(problems):
            final_string_4 += ' '
            


    if '*' in operator_str or '/' in operator_str:
        print('Error: Operator must be "+" or "-".')
    elif len(problems) > 5:
        print('Error: Too many problems.')
    elif any(x > 4 for x in max_number_length):
        print('Error: Numbers cannot be more than four digits.')
    elif alphabet_error == True:
        print('Error: Numbers must only contain digits.')
    else:
        le_grand_final_string = final_string_1 +'\n'+ final_string_2 +'\n'+ final_string_3
        if show_answers == True:
            le_grand_final_string += '\n'+ final_string_4
        print(le_grand_final_string)
    print('\n')
        
        


arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)

arithmetic_arranger(["3801 - 2", "123 + 49"])

arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])

arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])

arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])

arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])

arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])

arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])

arithmetic_arranger(["3 + 855", "988 + 40"], True)

arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

I don’t see anywhere that you’re returning a string?

I was trying to declare four empty strings at the top and then put them all together at the bottom, after the loop fills them out. I commented out some lines and tried saying return instead of print, but then it only prints the error messages. I’m kind of lost. Is my code just totally flawed?

What does your code look like when you try to use return instead of print? You must return strings, per the instructions.

Okay now I changed a few things and put all of the function calls inside print(f’{arithmetic_arranger(…)}') and now it shows up nicely but the tests still fail. Do I need to do this?

if you are now only returning and not printing, share your code with us

Did you check the dev console error (press F12) ?

It really helps if you show the full new code

Okay here’s my changed code. Now the error code tests work but all the other ones still do not. Thanks for help. I’m relatively new to Free Code Camp, so if there is a better way to share my code, I’m all ears. Thanks

def arithmetic_arranger(problems, show_answers=False):
    final_string_1 = ''
    final_string_2 = ''
    final_string_3 = ''
    final_string_4 = ''
    operator_str = ''
    max_number_length = []
    le_grand_number_str = ''
    le_grand_final_string = ''
    alphabet_error = False
    number_length_error = False
    for j in range(len(problems)):
        i = problems[j]
        top_number = i.split(' ', 1)[0]
        le_grand_number_str += top_number
        operator = i.split(' ', 2)[1]
        operator_str += operator
        bottom_number = i.split(' ', 2)[2]
        le_grand_number_str += bottom_number
        if not le_grand_number_str.isnumeric():
            alphabet_error = True
            break
        a = len(top_number)
        b = len(bottom_number)
        max_number_length.append(max(a, b))
        if any(x > 4 for x in max_number_length):
            number_length_error = True
            break 
        c = a - b
        if c >= 0:
            final_string_1 += ' ' * 2 + top_number
        elif c < 0:
            final_string_1 += ' ' * (2 - c) + top_number
        final_string_1 += ' ' * 4
        final_string_2 += operator
        if c >= 0:
            final_string_2 += ' ' * (c + 1) + bottom_number
        elif c < 0:
            final_string_2 += ' ' + bottom_number
        final_string_2 += ' ' * 4
        final_string_3 += '-' * (max(a, b) + 2)
        final_string_3 += ' ' * 4
        evaluated_number = str(eval(i))
        d = len(evaluated_number)
        final_string_4 += ' ' * (max(a, b) + 2 - d)
        final_string_4 += evaluated_number
        final_string_4 += ' ' * 4
            


    if '*' in operator_str or '/' in operator_str:
        le_grand_final_string ="Error: Operator must be '+' or '-'."
    elif len(problems) > 5:
        le_grand_final_string ='Error: Too many problems.'
    elif number_length_error == True:
        le_grand_final_string ='Error: Numbers cannot be more than four digits.'
    elif alphabet_error == True:
        le_grand_final_string ='Error: Numbers must only contain digits.'
    else:
        le_grand_final_string = final_string_1 +'\n'+ final_string_2 +'\n'+ final_string_3
        if show_answers == True:
            le_grand_final_string += '\n'+ final_string_4
    return(le_grand_final_string)

        


print(f'{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)}')

If you open the browser console you should see a more detailed output of the tests
For example:

AssertionError: '   3[32 chars]  988    \n- 698    - 3801    + 43    +  49   [100 chars]    ' 
             != '   3[32 chars]  988\n- 698    - 3801    + 43    +  49    +  [84 chars]1028'
`

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!

Ahhh thank you! This is very helpful. I got it to work. I was putting 4 more spaces on the end of each line. So I just had to move my final_string_1 += ’ ’ * 4 etc to an if statement that checks if j is less than len(problems) - 1, else: break.