Arithmetic arrragner code polish up

Hi everyone, I a new here, and in this lockdown I have started learning python to improve myself and develop new skills.
I am a beginner, and doing the scientific computing python course.
I have had a go at the first project, the arithmetic arranger, and I am looking for ways to improve my code:

maths = []

while True:
    a = input("enter first operand")
    b = input("enter operator")
    c = input("input second operand")
    d = (str(a), str(b), str(c))
    e = " "
    x = e.join(d)
    maths.append(x)
    n = input("have you finished entering the problem list? type 'y' for 'yes' or 'n' for 'no")
    if n == 'n':
        continue
    else:
        if n == 'y':
            break





def arithmetic_arranger(maths, results = 0) :

    if len(maths) > 5:
        print("Error, too manu problmes")
    else:
        operatorlist = []
        for i in maths:
            x = i.split()
            operatorlist.append(x[1])
    filteredmaths = list(filter(lambda z: z == "-" or z == "+", operatorlist))
    if len(filteredmaths) != len(operatorlist):
            print("Operator must be '+' or '-' ")
    else:
        operandlist = []
        for i in maths :
            x = i.split()
            operandlist.append(x[0])
            operandlist.append(x[2])
        filteredmaths2 = list(filter(lambda z: z.isdigit() == False, operandlist))
        if len(filteredmaths2) != 0 :
            print("Error: Numbers must only contain digits")

        else:
            filteredmaths3 = list(filter(lambda z: len(z) > 4, operandlist))
            if len(filteredmaths3) != 0 :
                print("Error: Numbers cannot be more than four digits.")
            else:
                for i in maths:
                    x = i.split()
                    topline = ("  " + x[0]).rjust(15)
                    print(topline.format(4), end="")
                print("")
                for i in maths:
                    x = i.split()
                    bottomline = (x[1] + " " + x[2]).rjust(15)
                    print(bottomline.format(4), end="")
                print("")
                for i in maths :
                    x = i.split()
                    if len(x[0]) >= len(x[2]):
                        print(((len(x[0])+1)*"-").rjust(15), end="")
                    else:
                        print(((len(x[2])+1 )* "-").rjust(15), end="")
                print("")
                if results == True :
                    for i in maths:
                        x = i.split()
                        b = str(int(x[0]) - int(x[2])).rjust(15)
                        c = str(int(x[0]) + int(x[2])).rjust(15)
                        if x[1] == "-" :
                            print(b.format(4), end="")
                        if x[1] == "+" :
                            print(c.format(4), end="")
                else :
                     print("   ")

#enter "True" as the second parameter into the arithmetic arranger to display answers
arithmetic_arranger(maths, True)

I would appreciate any advice on how tom polish the code up. I have purposely not have a look at what other people were writing whilst I was doing the project because I wanted to first write a functioning code entirely by myself with no hints to test my knowledge and only later seek advice for how to improve my solution

Does this pass all the tests?
If so, then you are free to look at other projects and see what they did different and if it seems like an improvement over your code.
If not, try to figure out where the issue is.
For a start, I doubt your code passes because:

  1. You are supposed to return a string - your code has NO return whatsoever
  2. Right now you are either printing error-messages or go deeper into an if-else. At the end, you are in a if-else-else-else I think. That’s something you should try to avoid, as it again makes it harder to follow what your code is doing.

One things you can definitly improve is on naming your variables.
Just calling them a, b, c, d, e, x and such makes your code REALLY hard to read, because as a reader (and potential future coworker) I would need to memorize what each letter refers to.
Same goes for loops i in maths is problematic because i is usually used for integers like index, but in your code it’s a string.
Instead use meaningful names, either written with camelCase or snake_case if necessary. It’s a bit more writing for you, but it will be super helpful in the long run. If you look at the code a week later, maybe for debugging - knowing what a variable is used for is super helpful.

Also while Python changes datatypes as needed, try to avoid it for better readability. Using results = False, as defaultvalue makes it easy to understand it’s a boolean and not an integer.

1 Like

The code was working and was passing all tests actually!. The only thing that was wrong was that if I entered too many problems it would display “Error too many problems” (as required by the challenge) but then it would also print out an error message from python regarding the function. So it sort of worked.
I added a return() at the end of it, and now everything works perfectly. So thank you for pointing that part out. and helping me fix that bug!

Now all the code works! (I am now curious as to why the function was sort of working anyway when I tested it but I’ll ponder on that later. I have also changed the variables name into something that is easier to understand for the reader, as you suggested.
Thank you for your help and useful tips!

If you go to the page of the problem, there should be a link to repl.it → you are supposed to write the code there, because it contains a pre-written set of tests and at the end when you pass all of them, you use the link to the project as proof you did the challenge.
You don’t actually need to enter you own problems that way :wink:

Also the reason you got the function error is because the code after the first if-else is not indented to be IN the else (like you did with all following if-else), hence the following code still executed, but because the operatorlist is created in the else (which is not executed by to many problems), it does not exist and runs into problems.