Advertisement
Guest User

Untitled

a guest
Jul 14th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. def contains(string, set):
  2.     return 1 in [c in string for c in set]
  3.  
  4. # Evaluate mathematical expression from string
  5. def calculate(expression):
  6.     if contains(expression, '_\'\"[];'):
  7.         return 'no'
  8.     try:
  9.         result = eval(float_expression, {"__builtins__": None}, safe_dict)
  10.         # if isinstance(result, float) and result.is_integer():
  11.         #   result = int(result)
  12.         # return result
  13.         if -1e-10 < result < 1e-10:
  14.             return '0'
  15.         elif -1e-10 < int(result) - result < 1e-10:
  16.             return str(int(result))
  17.         else:
  18.             return str(result)
  19.     except Exception, ex:
  20.         print ex
  21.  
  22. expression = raw_input()
  23. calculate(expression)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement