I am trying to count the number of brackets in an input math expression. if there are not right brackets than left brackets, program should prompt users to tell them an error:
"Unexpected right parenthesis between _ and _"
for example:
expression = ( ( 1 + 2 ) ) ) )
output: Unexpected right parenthesis between ( ( 1 + 2 ) ) and ) )
Question: how do I get to the index between ( ( 1 + 2 ) ) and ) ) to print out my desired output?
Below is the code that I have now. I appreciate anyone who can help me with this. Thank you.
tokenList = []
countLeft = 0
countRight = 0
mark1 = []
mark2 = []
expression = input("Enter an expression: ")
for i in expression.split(" "):
    tokenList.append(i)
for i in range(len(tokenList)): 
    if tokenList[i] == '(':
        countLeft += 1
    if tokenList[i] == ')':
        countRight += 1
if countLeft < countRight:
    for i in range(len(tokenList)):
        if tokenList[i] == '(': # this is to store the index  of '('
            mark1.append(i)
    for i in range(len(tokenList)):
        if tokenList[i] == ')': # this is to store the index of ')'
            mark2.append(i)
while (' and ')' in tokenList:
    tokenList.pop(mark1[-1])
    mark1.pop(-1)
    mark2.pop(0)
print("Unexpected right parenthesis between _ and _") 
if countRight < countLeft:
    print(f"There is {countLeft - countRight} unclosed left parenthesis in {expression}")