I need Better Code for my Python Arithmatic Formatter project #FreeCodeCamp

I need to improve this code to produce consistent results for a project submission on free code camp, I'm preferably looking for a method to solve these type of problems rather than a straight up solution to this problem.

def arithmetic_arranger(equations,ans='False'):
        if len(equations)>5: #Checking if more than 5 equations were given
            print("Error: Too many problems.")
        else:
                #Columns
            side1=list()
            sign=list()
            side2=list()
            #Initializing Column 1
            print(" ",end="")
            for i in equations:
                isplit=i.split(" ")
                side1.append(isplit[0])
                sign.append(isplit[1])
                side2.append(isplit[2])
            for i in side1:
                max_len=4
                if (len(i)>max_len): #Check for max num of digits
                    print("Error: Numbers cannot be more than four digits.")
                if(len(i)==max_len):
                    print(i, end="\t\t")
                if(len(i)==3):
                    print(" ",i, end="\t\t")
                if(len(i)==2):
                    print("  ",i, end="\t\t")
                if(len(i)==1):
                    print("   ",i, end="\t\t")
            print("\n",end=" ")
            a=0;
            for i in sign:
                print(i, end="")
                if (len(side2[a])>max_len): #Check for max num of digits
                    print("Error: Numbers cannot be more than four digits.")
                if(len(side2[a])==max_len):
                    print(side2[a], end="\t\t")
                if(len(side2[a])==3):
                    print("",side2[a], end="\t\t")
                if(len(side2[a])==2):
                    print(" ",side2[a], end="\t\t")
                if(len(side2[a])==1):
                    print(" ",side2[a], end="\t\t")
                a=a+1
            print("\n -----     ------      ------      ------")
            u=0;
            print("   ",end="")
            if ans==True:
                for i in sign:
                    if (i=='+'):
                        result=int(side1[u])+int(side2[u])
                        if (u==4):
                            print("",result, end="\t")
                        if (u==3):
                            print("",result, end="\t")
                        if (u==2):
                            print("",result, end="\t")
                        if (u==1):
                            print("",result, end="\t")
                        if (u==0):
                            print("", result, end="\t")
                    if (i=="-"):
                        result=int(side1[u])-int(side2[u])
                        if (u==4):
                            print("",result, end="\t")
                        if (u==3):
                            print("  ",result, end="\t")
                        if (u==2):
                            print("",result, end="\t")
                        if (u==1):
                            print("",result, end="\t")
                        if (u==0):
                            print("", result, end="\t")
                    u=u+1
                    print("   ", end="")
    arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"],True)
    #arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)

I need it took like this Output should look like so And follow these conditions

**[Output] 32 3801 45 123

  • 698 - 2 + 43 + 49

730     3799        88      172   

Repl Closed**

I don't know why this seems so hard, perhaps cause I don't find it interesting. I tried doing it myself but I gave up on account of I don't feel like find a solution to this trivial problem, but if anyone has experience with this I'll be glad if you could share it and some advice to help prevent me from facing similar problems.



Read more here: https://stackoverflow.com/questions/65706064/i-need-better-code-for-my-python-arithmatic-formatter-project-freecodecamp

Content Attribution

This content was originally published by Retro at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: