0

I currently have defined my object (is_matched) that contains a while loop and also the checking of a string. I am trying to pull out the while loop into it's own object but am having trouble completing the coding. I also need the while loop to continue running until a user enters 'q' into the input.

def is_matched():
while True: 
    expr = input("Enter string (q to quit): ") 
    if expr == "q":
        break
    stack = Stack() 
    for i in expr:
        if i == '{' or i == '(' or i == '[':
            stack.push(i)
        elif i == '}':
            if len(expr) != 0:
                c = stack.top()
                if c == '{':
                    stack.pop()
                else:
                    return "Unbalanced"
        elif i == ')':
            if len(expr) != 0:
                c = stack.top()
                if c == '(':
                    stack.pop()
                else:
                    return "Unbalanced"
        elif i == ']':
            if len(expr) != 0:
                c = stack.top()
                if c == '[':
                    stack.pop()
                else:
                    return "Unbalanced"
        else:
            return "Unbalanced"
            
    if len(stack) != 0:
        return "Unblanced"
    return "Balanced"
    result = is_matched()
    print(result)

I am trying to pull out the while loop from above and enter into its own object as follows:

def main():
while True: 
    expr = input("Enter string (q to quit): ") 
    if expr == "q":
        break
    else:
        return is_matched()
  • 1
    "but am having trouble completing the coding." Please define what exact problem you are having since this is unclear. – Poojan Jul 25 '20 at 17:04
  • So from what I understand you want to run your while loop until `q` comes. And then return result of all previous strings `Balanced` or `Unbalanced`. – Poojan Jul 25 '20 at 17:06
  • Yes, I am looking to pull the while loop into another object (main) – Nesto Madrigal Jul 25 '20 at 17:09

1 Answers1

0
  • your is_matched function will take one argument which will be the expression to evaluate.
def is_matched(expr):
    stack = Stack() 
    for i in expr:
        if i == '{' or i == '(' or i == '[':
            stack.push(i)
        elif i == '}':
            if len(expr) != 0:
                c = stack.top()
                if c == '{':
                    stack.pop()
                else:
                    return "Unbalanced"
        elif i == ')':
            if len(expr) != 0:
                c = stack.top()
                if c == '(':
                    stack.pop()
                else:
                    return "Unbalanced"
        elif i == ']':
            if len(expr) != 0:
                c = stack.top()
                if c == '[':
                    stack.pop()
                else:
                    return "Unbalanced"
        else:
            return "Unbalanced"
            
    if len(stack) != 0:
        return "Unblanced"
    return "Balanced"
  • main function.
def main():
    while True: 
        expr = input("Enter string (q to quit): ") 
        if expr == "q":
            break
        else:
            print(is_matched(expr))
Poojan
  • 3,366
  • 2
  • 17
  • 33