Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stack = []
- operators = ['+','-','*','/']
- do = ''
- def get_input():
- print('\nStack =',stack, end='\n')
- stack.append(input("> "))
- try:
- k = int(stack[-1])
- except ValueError:
- if stack[-1] not in operators:
- print("'", stack.pop(), "' is not a valid input.", sep='')
- get_input()
- while True:
- try:
- while stack[-1] not in operators:
- get_input()
- except IndexError:
- get_input()
- try:
- do = stack[-3] + ' ' + stack[-1] + ' ' + stack[-2]
- stack = stack[:-3]
- done = eval(do)
- print('\n',do, '=', done)
- stack.append(done)
- get_input()
- except IndexError:
- if stack[-1] in operators:
- print("Stack insufficient. '",stack.pop(),"' removed.", sep='')
- print(stack)
- get_input()
Advertisement
Add Comment
Please, Sign In to add comment