Arithmetic Formatter it is hard :o

God i can not do formatting properly, to be precise, returning side by side results.
And i have problem with return statement and logic. inside my code on every variable i made detail comment. pls help.

def arithmetic_arranger(problems, show_solutions=True):
     
    #in variable numbers_sorted i split problems into first number, operator, and second number    
    numbers_sorted =[]
    
    #this should be returned as last variable. this is problem
    final_product="" 
    
    
    if len(problems)   >5:
      'Error: Too many problems.'
    
    #spliting problems into number1 i[0] and number2 i[2] and operator i[1] 
    for i in problems:
        numbers_sorted.append(i.split(" "))
   
    
    for i in numbers_sorted:
         
        if i[0].isnumeric()==False:
            return "Error: Numbers must only contain digits."
        if i[2].isnumeric()==False:
            return "Error: Numbers must only contain digits."
        if i[1] !="+" and i[1]!="-":
            return "Error: Operator must be '+' or '-'."
        if len(i[0]) >=5 or len(i[2])   >=5:
            return "Error: Numbers cannot be more than four digits."
         
        
    
    
        
        #in results i do arimetics , first number + or minus second number
        result=""
        
        if i[1]=="+":
            result= int(i[0])+int(i[2])
             
        elif i[1]=="-":
            result= int(i[0])-int(i[2])
            
        #this is for rjust, no problem with this    
        width = max(len(i[0]), len(i[2]))+2
        
        #top line, first number and rjust
        top_line= i[0].rjust(width)
        
        #operator + second number.rjust
        middle_line= i[1] +i[2].rjust(width-1)
        #this is no problem
        only_line=""
        
        #and final line is just number from result converted to string and r.justed so it can easly be printed
        final_line = str(result).rjust(width)
          
        
        for z in range(width):
            only_line +="-"
        
      #HERE ARE PROBLEMS; 
      #first problem is that is no way how i can figure how to get results side by side..i got ok rjust, lines,
        #but they are printed one bellow other. pls help
      #second problem si -RETURN -how is this function working? when i put for example "print final product" i got what 
    #i want.. when i want to use "return"  i dont understand.. where ever i move itd, it is just prints nothing or confusion. 
    #so how to make return to work properly, and how to format those numbers side by side.. 
    
        if show_solutions:
             
            final_product= top_line +"\n" +middle_line +"\n"+only_line +"\n"+final_line
            print(final_product) 
        else:
            final_product =top_line +"\n" +middle_line +"\n"+only_line
            
    return(final_product)
        
    
      
       

         

The too many problems error is easy. You need to return the error. Here’s your output on top versus the expected for the formatting:

   32
- 698
-----
 -666
     1
- 3801
------
 -3800
  45
+ 43
----
  88
  123
+  49
-----
  172
  123
+  49
-----
  172
======================================================================
   32         1      45      123
- 698    - 3801    + 43    +  49
-----    ------    ----    -----
 -666     -3800      88      172

The alignment for each problem is fine, but you’re putting them on separate lines and not side by side. The problem is at the bottom where you’re constructing final_product. That conditional is running for every problem and not after all the problems have been processed. Or in simpler terms, you’re indentation is wrong.

But when you fix this, you still have problems with how you are building the individual lines, as you are overwriting the line variables each time you do a new problem (i.e. top_line = i[0].rjust(width)) instead of constructing an entire line by concatenating the substrings for each line (i.e. top_line += ' ' + i[0].rjust(width)).

Good luck.

thanks dude for help. i took your guidance, and with little help of the grey matter in my head, i fixed indentations(to be honest probably by luck) and did little changes of the code… i am almost there;
you can see that i got everything good, but i am failing on tests – i was reading on forum form other comments, that is because last line adds spaces, while is not needed -if i understood correctly.
well i ll need to figure that out tomorrow after the job … you can jump with advice :slight_smile: thanks again

def arithmetic_arranger(problems, show_solutions=True):
     
    #in variable numbers_sorted i split problems into first number, operator, and second number  - they are all strings
    numbers_sorted =[]

    #other variables - final product will be final return,
    final_product=""
    
    #result is an integer; product of the operation (+ or -) of first and second number
    result=""
    #first number
    top_line=""
    #middle line is operator (+ or - )+ second number
    middle_line=""
    #final line is a result converted back to string + r.just
    final_line="" 

    #this is only "----- " lines
    only_line="" 
    
    if len(problems)   >5:
      return 'Error: Too many problems.'
    
    #spliting problems into number1 i[0] and number2 i[2] and operator i[1] 
    for i in problems:
        numbers_sorted.append(i.split(" "))
   
    
    for i in numbers_sorted:
         
        if i[0].isnumeric()==False:
            return "Error: Numbers must only contain digits."
        if i[2].isnumeric()==False:
            return "Error: Numbers must only contain digits."
        if i[1] !="+" and i[1]!="-":
            return "Error: Operator must be '+' or '-'."
        if len(i[0]) >=5 or len(i[2])   >=5:
            return "Error: Numbers cannot be more than four digits."
         
        
    
    
        
        #in results i do arimetics , first number + or minus second number
        
        if i[1]=="+":
            result= int(i[0])+int(i[2])
             
        elif i[1]=="-":
            result= int(i[0])-int(i[2])
            
        #this is for rjust, no problem with this    
        width = max(len(i[0]), len(i[2]))+2
        
        #top line, first number and rjust
        top_line+= i[0].rjust(width)+"   "
        
        #operator + second number.rjust
        middle_line+= i[1] +i[2].rjust(width-1)+"   "
         
        
        
        #and final line is just number from result converted to string and r.justed so it can easly be printed
        final_line += str(result).rjust(width)+"   "
          
        only_line+=("-"*width)+"   "
        
        
     
  
    if show_solutions:
             
        final_product+= top_line +"\n" +middle_line +"\n"+only_line+"\n"+final_line
         
    else:
        final_product =top_line +"\n" +middle_line +"\n"+only_line

          
    return final_product

Check out the lstrip(), rstrip(), and strip() string methods for removing whitespace. Or, there are other ways to join the pieces of the lines that don’t result in extra whitespace on the end.

But yes, as you are looping, you are adding extra spaces on the end of each line after the last problem.

i cant believe i did it. test in replit displaying OK

cant believe that days ago i was about to quit, then step by step each day, i did it. even now when i compare my code to others -it is looking much smaller and easier to read… this is proof that you must be persistent in life to learn anything new. guys dont quit if it is looking hard at beginning.
thank you dude. i am so glad that i am giving few $ per month to freecodecamp. everyone should do the same, you guys are changing the world and with small contribution we are all together changing the world.

anyway i was experimenting on jupyter notebook, how and best to add .rstrip() to remove white space on the end. after hour experimenting -
and i just did it in final variable after each line -
" final_product+= top_line.rstrip() +"\n" +middle_line.rstrip() +"\n"+only_line.rstrip()+"\n"+final_line.rstrip() "

when i was doing that i expected because it is “+=” that it will delete white space after each iteration , and it only did on the last iteration? can you explain me why not deleting all white space only deleting on the end(where should be deleted), i dont understand that how that logic is working :slight_smile:

here is my code with #comments so everyone can easy dive into

def arithmetic_arranger(problems, show_solutions=False):
     
    #in variable numbers_sorted i split problems into first number, operator, and second number  - they are all strings
    numbers_sorted =[]

    #other variables - final product will be final return,
    final_product=""
    
    #result is an integer; product of the operation (+ or -) of first and second number - that #will happen latter
    result=""
    #first number
    top_line=""
    #middle line is operator (+ or - )+ second number
    middle_line=""
    #final line is a result(an integer) converted back to string + r.just
    final_line="" 

    #this is only "----- " lines
    only_line="" 
    
    if len(problems)   >5:
      return 'Error: Too many problems.'
    
    #spliting problems into number1    i[0]   and number2   i[2]   and operator   i[1]  
    for i in problems:
        numbers_sorted.append(i.split(" "))
   
    
    for i in numbers_sorted:
         
        if i[0].isnumeric()==False:
            return "Error: Numbers must only contain digits."
        if i[2].isnumeric()==False:
            return "Error: Numbers must only contain digits."
        if i[1] !="+" and i[1]!="-":
            return "Error: Operator must be '+' or '-'."
        if len(i[0]) >=5 or len(i[2])   >=5:
            return "Error: Numbers cannot be more than four digits."
         
        
    
    
        
        #in variable "result" i do arithmetics , first number + or - second number
        
        if i[1]=="+":
            result= int(i[0])+int(i[2])
             
        elif i[1]=="-":
            result= int(i[0])-int(i[2])
            
        #width will be used for rjust, 
   
        width = max(len(i[0]), len(i[2]))+2
        
        #top line, first number and rjust
        top_line+= i[0].rjust(width)+"    "
        
        #operator + second number.rjust
        middle_line+= i[1] +i[2].rjust(width-1)+"    "
         
        


        #and final line is just number from result converted to string and r.justed so it can easly be printed
        final_line += str(result).rjust(width)+"    "
          
        only_line+=("-"*width)+"    "
        
        
     #in final_product   i added on the end of each variable  .rstrip() to remove white space
   
    if show_solutions:
             
        final_product+= top_line.rstrip() +"\n" +middle_line.rstrip() +"\n"+only_line.rstrip()+"\n"+final_line.rstrip()
         
    else:
        final_product +=top_line.rstrip() +"\n" +middle_line.rstrip() +"\n"+only_line.rstrip()

          
    return final_product
        
    
             
         

thanks again to all

This code is in the loop and runs for every problem in the list. The += just adds whatever is in the RHS to what was in the LHS variable and then stores it in the LHS variable. It won’t affect whitespace.

This is outside the loop, so it runs once, after all the problems are in the lines and rstrip() removes all whitespace from the right end of the string (only). You set final_product = "" near the beginning, so the += here adds the new string from the right to the empty string, so you get just the new string. You could have used just = here with the same effect. So it does exactly what you need: take the 3 (or 4) lines, remove the extra spaces from the end of each line, join them with newlines and returns the string.

Now that you’ve got this, you may want to investigate using join() to arrive at the same result. Not that your way is wrong, but using join() in these situations is a common Python idiom you’re likely to see often in other people’s code.