Home > Back-end >  I did define but why asking checkBrackets not defined
I did define but why asking checkBrackets not defined

Time:04-02

I did define the class as below but it gives me error message says, name 'checkBrackets' is not defined.---> 50 result = checkBrackets(equation)

I did define the class as below but it gives me error message says, name 'checkBrackets' is not defined.

class Stack:
    def __init__(self):
        self.stack = []
        
    
    def push(self, item):
        self.stack.append(item)
    
    def isEmpty (self): 
        return len(self.stack) == 0
    
    def peek (self):
        if len(self.stack) != 0:
            return self.stack[-1]    
        
    def pop(self):
        if len(self.stack) != 0:
            return self.stack.pop(-1)
    

    ### 

    def checkBrackets(statement):
        
        stack = Stack()
        size = 0
        flag = "Wrong"
        
        for ch in statement:
            if ch in ('{','[','('):
                size  = 1
                statck.push(ch)
            elif ch in ('}',']',')'):
                size  = 1
                if stack.isEmpty():
                    return (flag, size)
                else:
                    left = stack.pop()
                    if (ch=="}" and left !="{") or (ch=="]" and left !="[") or (ch==")" and left !="(") : 
                        return (flag, size)

        if stack.isEmpty():
            flag ="OK"
            return (flag, size)
        else:
            return (flg, size) 
        

equation = input ("Enter_qauation: ")
result = checkBrackets(equation)
print ("%s_%d"%(result[0],result[1]))

CodePudding user response:

The function is defined, but it is defined in the class Stack so you need to call it as Stack.checkBrackets(equation). There were two more errors(spelling errors), so the final code looks like:

class Stack:
    def __init__(self):
        self.stack = []
        
    
    def push(self, item):
        self.stack.append(item)
    
    def isEmpty (self): 
        return len(self.stack) == 0
    
    def peek (self):
        if len(self.stack) != 0:
            return self.stack[-1]    
        
    def pop(self):
        if len(self.stack) != 0:
            return self.stack.pop(-1)
    

    ### 

    def checkBrackets(statement):
        
        stack = Stack()
        size = 0
        flag = "Wrong"
        
        for ch in statement:
            if ch in ('{','[','('):
                size  = 1
                stack.push(ch) # spelling correction
            elif ch in ('}',']',')'):
                size  = 1
                if stack.isEmpty():
                    return (flag, size)
                else:
                    left = stack.pop()
                    if (ch=="}" and left !="{") or (ch=="]" and left !="[") or (ch==")" and left !="(") : 
                        return (flag, size)

        if stack.isEmpty():
            flag ="OK"
            return (flag, size)
        else:
            return (flag, size) # spelling correction
        

equation = input ("Enter_equation: ")
result = Stack.checkBrackets(equation) # Changed Line
print ("%s_%d"%(result[0],result[1]))

Sample Output 1:

Enter_qauation: 
(1 2)=3
OK_2

Sample Output 2:

Enter_qauation: 
1 2=(3
Wrong_1
  • Related