Создание исключения
import operator
class Stack:
def __init__(self):
self._stack = []
def push(self, num):
return self._stack.append(num)
def pop(self):
try:
return self._stack.pop()
except IndexError:
raise IndexError('Empty')
OPERATORS = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.floordiv
}
class Calculator:
def __init__(self, array, operators=None):
self._array = array
self._operators = operators
def calculation(self, stack=None):
stack = Stack() if stack is None else stack
for item in self._array:
if item in self._operators:
second_num = stack.pop()
first_num = stack.pop()
stack.push(self._operators[item](first_num, second_num))
else:
try:
stack.push(int(item))
except ValueError as error:
raise ValueError(f'{error} - Item is not num!')
return stack.pop()
if __name__ == '__main__':
array = input().split()
stack = Stack()
print(Calculator(array, OPERATORS).calculation(stack))
Помогите создать свое исключение вместо:
except IndexError:
raise IndexError('Empty')