我收到属性错误,我不明白为什么。蟒蛇

时间:2016-02-24 17:47:16

标签: python class oop

以下是错误消息:

line 68, in isValid
    for symbol in (self.AllValidSymbols):
AttributeError: 'ExpressionEvaluation' object has no attribute 'AllValidSymbols'

我不明白,因为在我的代码(下面)中我使用它来将AllValidSymbols设置为一个列表。我知道代码有更多问题我只是不理解这个错误。

    class ExpressionEvaluation():
      stack = stack()
      def __int__(self):
      self.allinfixExpressions = []
      self.infixObj = InfixToPostfix()
      self.operatorList = ['+', '-', '*', '/', '%', '^', '@']
      self.AllValidSymbols = ['(', ')', '{', '}', '[', ']', '+', '-', '*', 

      '/', '%','^', '@', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']



def getExpressionsFromFile(self, filename):
    self.allinfixExpressions = []
    if (os.path.isfile(filename)):
        infile = open(filename, 'r')
        expr = infile.readline()
        while (expr != ""):#file is not empty
            expr = expr.strip()
            expr = expr.replace(" ", "")#remove spaces
            expr = expr.replace("//", "@")#remove spaces
            expr = expr.replace("**", "^")#remove spaces
            self.allinfixExpressions.append(expr)
            expr = infile.readline()#get next expression
        infile.close()
        return True
    else:
        return False
def doesNotMatch(self, left, right):
    answer = False
    if(left == '(' and right != ')'):
        answer = True
    elif (left == '[' and right != ']'):
        answer = True
    elif (left == '{' and right != '}'):
        answer = True

def isBalanced(self, expr):
    balanced = True
    index = 0
    expr = expr.strip()
    expr = expr.replace(" ", "")
    while(index < len(expr) and balanced):
        symbol = expr[index]
        if(symbol in '([{'):
            self.stack.push(symbol)
        elif (symbol in ')]}'):
            if(self.stack.is_empty()):
                balanced = False
            else:
                item = self.stack.pop()
                if (self.doesNotMatch(item, symbol)):
                    balanced = False
        index += 1
    if(balanced and self.stack.is_empty()):
        return True
    return False

def removeSymbol(self, symbol, exprList):
    n = exprList.count(symbol)
    for i in range(n):
        exprList.remove(elem)
def isValid(self,expr):
    exprList = list(expr)
    for symbol in (self.AllValidSymbols):
        self.removeSymbol(symbol, exprList)
    exprList = list(infixList)
    for elem in tempList:
        if(elem.isdigit()):
            self.removeElement(elem, PostfixList)
    if(postfixList == []):
        return True
    else:
        return False
def evaluateExpressions(self):
   # print(self.allinfixExpressions)
   if (self.allinfixExpressions == []): #empty list
       print("the input list is empty! there is nothing to evaluate.")
   else:
       for expr in self.allinfixExpressions:
           balanced = self.isBalanced(expr)
           valid = self.isValid(expr)
           if (balanced and valid):#good to go
               postfixlist = self.InfixToPostfix(expr)
               postfixExpr = self.getPostfix(postfixlist)
               result = self.evaluatePostfix(postfixlist)
               self.printresult(expr, postfixList)
           else:
               print("not balanced or valid.")


    def main():
     system = ExpressionEvaluation()
     goodFile = system.getExpressionsFromFile('infixexpressions5.txt')
     if(goodFile):
       system.evaluateExpressions()
   main()

1 个答案:

答案 0 :(得分:2)

这是因为您的初始化永远不会运行,您在__init__方法名称中输入了错字。