Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I do not find the difference between my result and the asked result. Can someone help me?

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    string_first=[]
    string_operand=[]
    string_second=[]
    fourth_line=[]
    solution=[]
    list_of_length=[]
    first_line=""
    second_line=""
    string_divisor=""
    error=""
    length=int()
    result=int()

    #ERRORCheck if more then 5 problems
    if len(problems) > 5:
        error='Error: Too many problems.'
        return error
    #Split the strings into their elements
    [string_first.append((x.split()[0])) for x in problems]
    [string_operand.append((x.split()[1])) for x in problems]
    [string_second.append((x.split()[2])) for x in problems]
    #Loop over all problems
    for x in range(len(problems)):
        #ERRORCheck if there are only digits
        for char in string_first:
            if char.isdigit()==True:
                continue
            else:
                error="Error: Numbers must only contain digits."
                return error
        for char in string_second:
            if char.isdigit()==True:
                continue
            else:
                error="Error: Numbers must only contain digits."
                return error
        #ERRORCheck if more then 4 digits
        if int(len(string_first[x]))>4:
            error="Error: Numbers cannot be more than four digits."
            return error
        if int(len(string_second[x]))>4:
            error="Error: Numbers cannot be more than four digits."
            return error
        #Search for the longest of the two operands
        else:
            if int(len(string_first[x]))>=int(len(string_second[x])):
                length=int(len(string_first[x]))
            elif int(len(string_first[x]))<int(len(string_second[x])):
                length=int(len(string_second[x]))
            list_of_length.append(length)
    #Definition of the first and the second line of the calculation
    for x in range(len(problems)):
        if x < len(problems):
            first_line+=string_first[x].rjust(list_of_length[x]+2)+"    "
            second_line+=string_operand[x]+" "+string_second[x].rjust(list_of_length[x])+"    "    
        else:
            first_line+=string_first[x].rjust(list_of_length[x]+2)
            second_line+=string_operand[x]+" "+string_second[x].rjust(list_of_length[x])     
        #Definition of the dashes  
        string_divisor=""   
        for x in range(list_of_length[x]+2):
            string_divisor+="-"
        string_divisor+="    "
        fourth_line.append(string_divisor)
    #Calculation of the result of each problem
    for x in range(len(problems)):
        if string_operand[x]=="+":
            result=int(string_first[x])+int(string_second[x])
        elif string_operand[x]=="-":
            result=int(string_first[x])-int(string_second[x])
        else:
            error="Error: Operator must be '+' or '-'."
            return error
        solution.append(str(result).rjust(length+2))
    #Converting each line into a string
    first="".join(first_line)
    second="".join(second_line)
    third="".join(solution)
    fourth="".join(fourth_line)
    #Printing the the result
    if not error:
        answer=f"{first}\n{second}\n{fourth}"
        #answer=first+"\\n"+second+"\\n"+fourth
    if show_answers==True:
        answer=f"{first}\n{second}\n{string_divisor}\n{third}"
    #print(first+"\\n"+second+"\\n"+fourth)
    return answer

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:136.0) Gecko/20100101 Firefox/136.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

How were you comparing the two? Did you take a look at the browser’s console? There’s more details about failing tests there.

browser console = F12? I tried, but wasn’t able didn’t get any information out of it. I also don’t know what to press to see the information i need in the console.

i copied and pasted the test code and saw the asked result in the preview. additionally i printed the answer from the test code and compared both answers in the preview.

Taking as an example one of the outputs:

AssertionError: '   3[32 chars]  988    \n- 698    - 3801    + 43    +  49   [44 chars]1028' != '   3[32 chars]  988\n- 698    - 3801    + 43    +  49    +  [84 chars]1028'
-    32         1      45      123      988    
?                                          ----
+    32         1      45      123      988
- - 698    - 3801    + 43    +  49    +  40    
?                                          ----
+ - 698    - 3801    + 43    +  49    +  40
- -----    
+ -----    ------    ----    -----    -----
-  -666-3800   88  172 1028+  -666     -3800      88      172     1028?      +++++        +++  ++++     ++++

Lines starting with - are returned by fuction, lines starting with + are expected, the lines with ? can show where’s the specific difference.

You can also pass the string returned by function, to the repr function, to be able to compare the expected string from test cases more directly - ie. print(repr(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)))

when you run the tests the extra infos from the tests appear in the console


I think i have switched some settings of the console because i don’t see anything in there.

that is not the browser console, or at least it’s not included in the screenshot if you have it open

oh i thought it would be the console at the bottom on the screenshot? This is what appears if i press F12 as recommended in the text.

what does appear if you press F12? you forgot the screenshot

no i would say the “white space” at the bottom of the screenshot(there is also written console somewhere) i sent comes if i press F12

oh, yeah, sorry, I missed that.

Yeah, that one, run the tests when that is open

i did but nothing appears. that’s why i think there are settings changed of the console…?

in the right part it looks like you are in the debug tab, I don’t know what the words mean but maybe you need to be in a different one

your code makes output in the console. You should see some alerts even with not running the tests

Enable the first four sections, you can see 7 and 11 messages waiting there. Enable the first four though.

ok i did but still don’t know what the answer inside the console says to me. But meanwhile i have managed the first 9 problems!
i attach the new screenshot with the first four enabled.

Thank you! This repr() helped me a lot! But unfortunally the image of your console output differs a lot of mine…

Copy and paste the full content of your console output here.

And your new code.

Thank you for your help! I got it. But I don’t want to post the whole code here…that would be the solution for everyone…
I hope i will understand the console once.

the console output is not your code, so sharing it would not be giving the solution away

anyway, infos on the console output:

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!