Https://repl.it/@emericftfabi/boilerplate-arithmetic-formatter-1#arithmetic_arranger.py

Tell us what’s happening:
This is my project Arithmetic Formatter

All the requirements are functional but it doesn’t seem to pass the test on repl.it
I’d like to know how to print those problems horizontally, please?
And if someone can tell me why it failed the test please ?
Your code so far

import re
def arithmetic_arranger(problems,answer=False):
    counter=0
    for operations in problems :
      counter+=1
      op = re.findall(r"\+|\-|\*|\/",operations)[0]
      at = "".join(operations.replace(op,'').split())
      if at.isdigit():
        if op=="+" or op=="-":
          a=re.findall(r"\d+",operations)[0]
          b=re.findall(r"\d+",operations)[1]
          x=int(a)
          y=int(b)
          f_result=x+y
          s_result=x-y
        
          if len(a) < 5 and len(b) < 5:
            if len(a) > len(b):
              term_sec= op+' '+' '*(len(a)-len(b))+b
            else:
              term_sec= op+' '+b
            lon=len(term_sec)
            pos='{:>'+str(lon)+'}'
            p = pos.format(a)+'\n'+pos.format(term_sec)+'\n'+'-'*lon
            if counter <6:
              if answer==False:
                print(p)
                print('\n')
              else:
                if op=="+":
                  print(p)
                  print(pos.format(f_result))
                  print('\n')  
                elif op=="-":
                  print(p)
                  print(pos.format(s_result))
                  print('\n')
            else: 
              print("Error: Too many problems.")
          else:
            print("Error: Numbers cannot be more than four digits.\n")
        else:
          print("Error: Operator must be '+' or '-'.\n")
      else:
        print("Error: Numbers must only contain digits.\n")
arithmetic_arranger(["13 + 1","23+ 456342","34445 + 344445","123 + 23","34+55","456-678"],True)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

Welcome to the forum ^^
For a start, please give your topic a meaningful title next time. Posting the link to your project as title doesn’t work.

I have to copy-paste the title into my browser to access your code. Instead post the link within your text next time.

Also I can’t help you because if I go to the repl.it with the link in the title, the arithmetic_arranger() is within another function AA() and that just breaks the program, resulting in no output whatsoever.

Without that outer function, I could at least look at the error message which usually gives insight into why you are failing the test.

If you could fix that, a look at the error message might help understanding where the problem might be.

Also if you are asking of how to write problem horizontally: by creating each LINE for the output (first line containing all the first numbers of the problem, second line containing, third line…) and combining those lines with “\n” (escape character for “newline”).

Thank you so much for your answer. And sorry for the mistakes, was my first post.
I finally discover what was wrong with my code, I changed it completely and I think is OK now .

This is my new code :

def arithmetic_arranger(problems,answer=False):
    f_row =[]
    s_row = []
    t_row = []
    r_row = []
    
    if len(problems)>5:
        return "Error: Too many problems."
    else:
        for operations in problems:
            term_cal=operations.split()
            a=term_cal[0]
            b=term_cal[2]
            op=term_cal[1]
            
            if a.isdigit() and b.isdigit():
                if len(a)<5 and len(b)<5:
                    if op in "+-":
                        result=eval(operations)
                        r= str(result)
                        if len(a)<len(b):
                            term_a = ' '*(len(b)-len(a)+2)+a
                            term_b = op+' '+b
                            term_c = '-'*(len(b)+2)
                            term_d = ' '*(len(term_c)-len(r))+r
                        else:
                            term_a = '  '+a
                            term_b = op+' '*(len(a)-len(b)+1)+b
                            term_c = '-'*(len(a)+2)
                            term_d = ' '*(len(term_c)-len(r))+r
                        f_row.append(term_a)
                        s_row.append(term_b)
                        t_row.append(term_c)
                        r_row.append(term_d)
                    else:
                        return "Error: Operator must be '+' or '-'."
                else:
                    return "Error: Numbers cannot be more than four digits."
            else:
                return "Error: Numbers must only contain digits."
    if len(problems) == 1:
        line_a= f_row[0]
        line_b= s_row[0]
        line_c= t_row[0]
        line_d= r_row[0]
    elif len(problems) == 2:
        line_a= f_row[0]+' '*4+f_row[1]
        line_b= s_row[0]+' '*4+s_row[1]
        line_c= t_row[0]+' '*4+t_row[1]
        line_d= r_row[0]+' '*4+r_row[1]
    elif len(problems) == 3:
        line_a= f_row[0]+' '*4+f_row[1]+' '*4+f_row[2]
        line_b= s_row[0]+' '*4+s_row[1]+' '*4+s_row[2]
        line_c= t_row[0]+' '*4+t_row[1]+' '*4+t_row[2]
        line_d= r_row[0]+' '*4+r_row[1]+' '*4+r_row[2]
    elif len(problems) == 4:
        line_a= f_row[0]+' '*4+f_row[1]+' '*4+f_row[2]+' '*4+f_row[3]
        line_b= s_row[0]+' '*4+s_row[1]+' '*4+s_row[2]+' '*4+s_row[3]
        line_c= t_row[0]+' '*4+t_row[1]+' '*4+t_row[2]+' '*4+t_row[3]
        line_d= r_row[0]+' '*4+r_row[1]+' '*4+r_row[2]+' '*4+r_row[3]
    elif len(problems) == 5:
        line_a= f_row[0]+' '*4+f_row[1]+' '*4+f_row[2]+' '*4+f_row[3]+' '*4+f_row[4]
        line_b= s_row[0]+' '*4+s_row[1]+' '*4+s_row[2]+' '*4+s_row[3]+' '*4+s_row[4]
        line_c= t_row[0]+' '*4+t_row[1]+' '*4+t_row[2]+' '*4+t_row[3]+' '*4+t_row[4]
        line_d= r_row[0]+' '*4+r_row[1]+' '*4+r_row[2]+' '*4+r_row[3]+' '*4+r_row[4]
        
        
    if answer == True:
        arranged_problems= line_a+'\n'+line_b+'\n'+line_c+'\n'+line_d
    else:
        arranged_problems= line_a+'\n'+line_b+'\n'+line_c
    return arranged_problems

And this is the link(hope I paste it on the right place now :grinning:):
https://repl.it/@emericftfabi/boilerplate-arithmetic-formatter#arithmetic_arranger.py

This is the ouput of the console:

 python main.py
   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
......
----------------------------------------------------------------------
Ran 6 tests in 0.001s

OK

Any advice to make my code(or program) faster will be grateful
Thanks!

So you pass the tests, that’s good ^^

One thing you definitly can improve is creating the output - instead of having this long if-elif-else chain, just have one for-loop in range(len(problems)) to add the values for each line.
You could also turn line_a, line_b… into an array and fill them with another for-loop if you want.

I’ve edited your post 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 (’).

Ok, thanks you.
sorry for all that, I’m new to this