-3
Вот сообщение об ошибке:Я получаю ошибку атрибута, и я не понимаю, почему. Python
line 68, in isValid
for symbol in (self.AllValidSymbols):
AttributeError: 'ExpressionEvaluation' object has no attribute 'AllValidSymbols'
Я не понимаю, потому что в моем коде (ниже) я intilized его иметь 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()
Исправить ваши отступы. – idjaw