Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

It’s not passing me the tests even though it prints what it’s supposed to.

I’m very new to Python, so I know it’s not the best code. But I don’t understand why it won’t accept my tests, even though it returns exactly what it says it should. Even when I print what it says is the response, the console displays exactly the same thing as my code.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    lista=["","","",""]
    length=len(problems)
    sumrest=["+","-"]
    for pro in problems:
        pro2=pro.split()
        try:
            a=int(pro2[0])
            b=int(pro2[2])
        except ValueError:
            problems='Error: Numbers must only contain digits.'
            break
        if pro2[1] in sumrest and length<6 and len(pro2[0])<5 and len(pro2[2])<5:
            #---------- arreglos -----------
            line1="  "+" "*(len(pro2[2])-len(pro2[0]))+pro2[0]
            lista[0]+=line1+"    "
            line2=pro2[1]+" "*(len(line1)-len(pro2[2])-1)+pro2[2]
            lista[1]+=line2+"    "
            line3="-"*len(line2)
            lista[2]+=line3+"    "
            if show_answers==True:
                if pro2[1]=="+":
                    result=a+b
                else:
                    result=a-b
                line4=" "*(len(line2)-len(str(result)))+str(result)
                lista[3]+=line4+"    "
                problems=lista[0]+"\n"+lista[1]+"\n"+lista[2]+"\n"+lista[3]
            else:
                problems=lista[0]+"\n"+lista[1]+"\n"+lista[2]
            #---------- arreglos -----------
        elif pro2[1] not in sumrest:
            problems="Error: Operator must be '+' or '-'."
            break
        elif length>5:
            problems='Error: Too many problems.'
            break 
        else:
            problems='Error: Numbers cannot be more than four digits.'
            break    
    for i in range(4):
        lista[i].strip()
    return problems
print(f'\n{arithmetic_arranger(["3 + 855", "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/137.0.0.0 Safari/537.36 OPR/121.0.0.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

looking at the browser console as suggested offers insight on why the tests are failing

for example

AssertionError: '    3      988    \n+ 855    +  40    \n-----    -----    \n  858     1028    '
             != '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'

the first line is yours, the second is the expected. It looks like there are extra spaces at the end of each line

thanks i know now whats the problem, the for i in range(4): lista[i].strip() funcion in the last part its badly place, and its not doing nothing. Thanks for the reply