kuzar

#073 - Easy

Jul 7th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. stack = []
  2. operators = ['+','-','*','/']
  3. do = ''
  4.  
  5. def get_input():
  6.     print('\nStack =',stack, end='\n')
  7.     stack.append(input("> "))
  8.    
  9.     try:
  10.         k = int(stack[-1])
  11.    
  12.     except ValueError:
  13.         if stack[-1] not in operators:
  14.             print("'", stack.pop(), "' is not a valid input.", sep='')
  15.         get_input()
  16.    
  17. while True:
  18.  
  19.     try:    
  20.         while stack[-1] not in operators:
  21.                 get_input()
  22.                
  23.     except IndexError:
  24.         get_input()
  25.              
  26.                
  27.     try:
  28.         do = stack[-3] + ' ' + stack[-1] + ' ' + stack[-2]
  29.         stack = stack[:-3]
  30.         done = eval(do)
  31.         print('\n',do, '=', done)
  32.         stack.append(done)
  33.        
  34.         get_input()
  35.        
  36.     except IndexError:
  37.         if stack[-1] in operators:
  38.             print("Stack insufficient. '",stack.pop(),"' removed.", sep='')
  39.             print(stack)
  40.         get_input()
Advertisement
Add Comment
Please, Sign In to add comment