Есть стек с перегрузкой операторов для вычисления формулы в постфиксной записи.
class Stack:
def __init__(self):
self.storage = []
def push(self, arg):
self.storage.append(arg)
def pop(self):
return self.storage.pop()
def clean(self):
del self.storage[:]
return self.storage
def is_empty(self):
return self.storage == []
def __str__(self):
return str(self.storage)
def __iter__(self):
for x in range(len(self.storage)):
yield x
def __iadd__(self, other):
self.storage[-2] += other
return self
def __isub__(self, other):
self.storage[-2] -= other
return self
def __imul__(self, other):
self.storage[-2] *= other
return self
def __idiv__(self, other):
self.storage[-2] /= other
return self
def __ipow__(self, other):
self.storage[-2] **= other
return self
if x == "+":
answer += answer.storage[-1]
answer.pop()
elif x == "-":
answer -= answer.storage[-1]
answer.pop()
elif x == "*":
answer *= answer.storage[-1]
answer.pop()
elif x == "/":
answer /= answer.storage[-1]
answer.pop()
elif x == "**":
answer **= answer.storage[-1]
answer.pop()
Все отлично как нужно работает кроме деления.
answer = answer / answer.storage[-1]
TypeError: unsupported operand type(s) for /: 'Stack' and 'float'
Что делать?