Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I can get result for each test for “Build and Arithmetic Formatter Project” and result looks correct on Console view; however when I click on box "Run the Tests(Ctrl+Enter)), all fails and console doesn’t show any particular error

Your code so far

def arithmetic_arranger(problems, show_answers = False):
    
    sumN = None
    result = ''

    if len(problems) > 5:
        return (print('Error: Too many problems.'))
    
    num1L = [i.split()[0] for i in problems]
    signL = [i.split()[1] for i in problems]
    num2L = [i.split()[2] for i in problems]

    
    line1 = num1L[0].rjust(((max(len(num1L[0]), len(num2L[0]))))+2)
    line2 = signL[0] + ' ' + num2L[0].rjust(max(len(num1L[0]), len(num2L[0])))
    line3 = '--' + ('-' * max(len(num2L[0]), len(num1L[0]))) + '    '


    for i in range(len(num1L)):
        
        if len(num1L[i]) > 4 or len(num2L[i]) > 4:
            return (print ('Error: Numbers cannot be more than four digits.' ))
            
        
        elif num1L[i].isdigit() == False or num2L[i].isdigit() == False:
            return (print('Error: Numbers must only contain digits.'))

        
        elif signL[i] != '+' and signL[i] != '-':
            retturn (print("Error: Operator must be '+' or '-'."))
            
        
    if show_answers:
        try:
            if signL[0] == '+':
                sumN = int(num1L[0])+int(num2L[0])
            elif signL[0] == '-':
                sumN = int(num1L[0])- int(num2L[0])

            suml = str(sumN).rjust(len(line3)-4) + '    '
        except:
            return (print("Exception happened in calculation sum"))
            

    for i in range(1, len(num1L)):
        line1 += '      ' + num1L[i].rjust(max(len(num1L[i]), len(num2L[i])))
        line2 += '    ' + signL[i].rjust(0) + ' ' + num2L[i].rjust(max(len(num1L[i]), len(num2L[i])))
        line3 += '--' + ('-' * max(len(num2L[i]), len(num1L[i])))+'    '
        line3L = line3.split()
        #print(line3,'\n', line3L)


        if show_answers:
            if signL[i] == '+':
                sumN = int(num1L[i])+int(num2L[i])
            else:
                sumN = int(num1L[i])- int(num2L[i])

            suml += str(sumN).rjust(len(line3L[-1]))+'    '
    
        sumL = suml.rstrip()
    if show_answers:
        result = line1+'\n'+line2+'\n'+line3.rstrip()+'\n'+sumL+'.'
        return result
        
    else:
        result = line1+'\n'+line2+'\n'+line3.rstrip()+'.'
        return result

#print(f'\n{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)}')

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

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Open the browser console with F12 for more details from the tests

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!

But also just printing, why there are periods next to numbers in your code?

Thank you for your input. I edited the end of the code to compare result with expected, but a simple test, I still see no diff, following is what I have changed

 if show_answers:
        result = line1+'\n'+line2+'\n'+line3.rstrip()+'\n'+sumL
        #return result
        
    else:
        result = line1+'\n'+line2+'\n'+line3.rstrip()
        #return result

    expected ='  3801      123\n-    2    +  49\n------    -----'
    assert result == expected
    
    return (print(f"{result}"'\n'f"{expected}"))


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

output as follows

have you checked the brower console?

if your function returns None you can’t pass any test

yes, the console doesn’t show any result on the bottom, how I can fix it? I see the output on top but not bottom

please open the browser console with F12, when you run the tests there will be a more detailed output

getting the following error which I don’t know how to fix it, doesn’t seem part of my coding

After I hit F12

Welcome to the forum @katayoun

The parts like this with - and + are ones you want to inspect.

The ? is showing you have a .
This is not expected by the tests.

Happy coding

Thank you for your help, finally got it fixed :slight_smile:

1 Like