fork download
  1. import operator as op, sys
  2.  
  3. table = {'+' : op.add, '-' : op.sub, '*' : op.mul, '/' : op.truediv}
  4.  
  5. def rpn(exp):
  6. stack = []
  7. for token in exp.split():
  8. try: stack += [int(token)]
  9. except ValueError:
  10. try: stack += [float(token)]
  11. except ValueError:
  12. stack = stack[:-2] + [table[token](*stack[-2:])]
  13. return stack[-1]
  14.  
  15. if __name__ == "__main__":
  16. while True:
  17. exp = input("RPN > ")
  18. if exp == "quit":
  19. sys.exit()
  20. else:
  21. try:
  22. print("{:.4f}".format(rpn(exp)))
  23. except:
  24. print("不正な入力です。")
  25. finally:
  26. exp
  27.  
Runtime error #stdin #stdout #stderr 0.15s 23624KB
stdin
Standard input is empty
stdout
RPN > 
stderr
Traceback (most recent call last):
  File "./prog.py", line 17, in <module>
EOFError: EOF when reading a line