0

The code below was created by me with the help of many SO veterans:

The code takes an entered math expression and splits it into operators and operands for later use. I have created two functions, the parsing function that splits, and the error function. I am having problems with the error function because it won't display my error messages and I feel the function is being ignored when the code runs. An error should print if an expression such as this is entered: 3//3+4,etc. where there are two operators together, or there are more than two operators in the expression overall, but the error messages dont print. My code is below:

def errors():

    numExtrapolation,opExtrapolation=parse(expression)
    if (len(numExtrapolation) == 3) and (len(opExtrapolation) !=2):
        print("Bad1")
    if (len(numExtrapolation) ==2) and (len(opExtrapolation) !=1):
        print("Bad2")


def parse(expression):
    operators= set("*/+-")
    opExtrapolate= []
    numExtrapolate= []
    buff=[]
    for i in expression:
        if i in operators:
            numExtrapolate.append(''.join(buff))
            buff= []
            opExtrapolate.append(i)
            opExtrapolation=opExtrapolate
        else:
            buff.append(i)
    numExtrapolate.append(''.join(buff))
    numExtrapolation=numExtrapolate
    #just some debugging print statements
    print(numExtrapolation)
    print("z:", len(opExtrapolation))
    return numExtrapolation, opExtrapolation

    errors()

Any help would be appreciated. Please don't introduce new code that is any more advanced than the code already here. I am looking for a solution to my problem... not large new code segments. Thanks.

1 Answer 1

1

The errors() function is called after parse() returns because it appears inside the body of parse(). Hopefully that is a typo.

For this particular input, numExtrapolate is appended with an empty buffer because there is no operand between / and /. That makes its length 4 and your check for Bad1 fails. So put a check like this

 if buff:
     numExtrapolate.append(''.join(buff))
Sign up to request clarification or add additional context in comments.

6 Comments

Ok. How abt print(len(numExtrapolate)). What do u see there.
I do get 4 for numex, even though it should be 3
Yes. Because one of them is empty string that is betwn //. Sorry cant chat from phone
I dont understand how the code you entered is a check for errors? I am new to python so I have never seen an "if buff" statement that doesnt have a booleon after it. Also In the argument of the if statement. What does that line mean. I know I have that line in my original code but I am not sure what the .append part does exactly even though I searched it up.
if buff is equivalent to if len(buff) > 0. The check prevents addition of empty buffers that appear betwn consecutive ops
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.