Arithmetic Arranger Problem - help!

Can someone help me to see how to fix this code? I already passed by four tests, and i think the main problem is the extra space in the beginning of the second line, and I don’t know how to fix. I will be greatfull for any advices and improvements!

def arithmetic_arranger(problems, choice=False):
    
    
    arranged_problems=""
    results=[]
    if (len(problems) > 5):
      arranged_problems ="Error: Too many problems."       
      return arranged_problems
    else:
        pass 
    
      
        
    for i in problems:     #deve ser - ou +  
        x=i.split()
        
        if (x[1]== "+"or x[1] =="-"):
            pass
                    
        else:
            arranged_problems="Error: Operator must be '+' or '-'."
            return arranged_problems
        
    for i in problems:
        x=i.split()
        
        x1_len=len(x[0])
        x2_len=len(x[2])
        
        if (x1_len > 4 or x2_len > 4):
            
            
            arranged_problems="Error: Numbers cannot be more than four digits."
            return arranged_problems
        else:
            pass
    

    for i in problems:
        x=i.split()
        a=x[0].isnumeric()
        b=x[2].isnumeric()
        
        
        if (a == True and b == True):
            pass
        
        else:
            arranged_problems="Error: Numbers must only contain digits."
            return arranged_problems


    linha1=''
    linha2=''
    linhadiv=''
    linharesult=''




    #resultados
    for i in problems:
            x=i.split()
            if x[1]=='+':
                y=(int(x[0])+int(x[2]))
                results.append(y)
            if x[1]=='-':
                y=(int(x[0])-int(x[2]))
                results.append(y)  
           
   
          
    for i in problems:      
        x = i.split()
        dig1=(x[0])
        len1=len(dig1)
        
        dig2=(x[2])
        len2=len(dig2)
        
        if (len1>len2):
            
            
        
            linha1+="  "+ x[0]+"    "
        else:
            space=len2-len1
            linha1+=" "*space+"  "+x[0]+"    "
        

    



    #segunda linha

    for i in problems:
        x = i.split()
        dig1=(x[0])
        len1=len(dig1)
        
        dig2=(x[2])
        len2=len(dig2)
        
        
        if len1 > len2:
            num1=len1-len2
            num2=num1+1
            linha2+=x[1]+" "*num2+x[2]+"    "
            
        else:    
            linha2+=x[1]+" "+x[2]+"    "
       
    
        
 #linha divisoria       
    for i in problems:
        x = i.split()
        dig1=(x[0])
        len1=len(dig1)
        
        dig2=(x[2])
        len2=len(dig2)
        
        if (len1 > len2):
            div=len1+2
            linhadiv+="-"*div + "    "
            
        else:
            linhadiv+="--"+"-"*len2+"    "
    
    
        
        
            
    if (choice==True):
       
        for i in range(len(problems)):
           xitem=problems[i].split()
           xlen1=len(xitem[0])
           xlen2=len(xitem[2])
           
           if (xlen1>xlen2):
               espace=xlen1+2
               string=str(results[i])
               lstr=len(string)
               esp=espace-lstr
               
               linharesult+=(" "*esp+string+"    ")
           else:
              espace=xlen2+2
              string=str(results[i])
              lstr=len(string)
              esp=espace-lstr
              linharesult+=" "*esp+string+"    "
            
             

              
           
    else:
      pass
           
    for i in linha1:
      arranged_problems+= i

    
    else:
        arranged_problems+="\n"
    for i in linha2:
      arranged_problems+= i
    else:
        arranged_problems+="\n"

    
    for i in linhadiv:
      arranged_problems+= i
    else:
        arranged_problems+="\n"
        

     

    for i in linharesult:
      arranged_problems+= i
    else:
      arranged_problems+="\n"
    return arranged_problems  

  

Thanks!

Please provide a link to your replit so we can run the code and actually see the errors.

Also for a first tipp: you can just add string together, no need to write loops for every single character.

1 Like

Thank you Jagaya, i did it before, but i changed it to try a different result. I will do this again! here’s the link:

https://replit.com/@lucasqvdo/boilerplate-arithmetic-formatter#arithmetic_arranger.py

arranged_problems=(linha1, linha2,linhadiv,linharesult)

That’s a tuple, not a string :wink:
You want linha1+linha2 and so on.

1 Like

Done… but it still doesn’t pass on the tests… any more suggestions?

Don’t mark a topic as solved until it is solved :wink:
As for the tests, here is the error message in color:

-   1         1
+   1         1    
?              ++++
- + 2    - 9380
+ + 2    - 9380    
?              ++++

You got trailing spaces at the end of the lines.

2 Likes