Skip to content

Instantly share code, notes, and snippets.

@rezkam
Created October 16, 2013 00:04
Show Gist options
  • Save rezkam/7000587 to your computer and use it in GitHub Desktop.
Save rezkam/7000587 to your computer and use it in GitHub Desktop.
Simple Calculator for Python Work group
def add(x, y):
return x + y
def sub(x, y):
return x - y
def mul(x, y):
return x * y
def div(x, y):
return x / y
def calculate(x, func, y):
try:
return func(x, y)
except (NameError, TypeError) :
return 'func name is Wrong!use add, sub, mul and div'
except ZeroDivisionError:
return 'Division By Zero Is Not Allowed!!!'
except Exception:
return 'Unknown Error!'
while True:
try:
x, func , y = input()
except Exception:
print 'Please Enter Correct Input! Use flowing format number1,function,number2'
continue
print calculate(float(x), func, float(y))
#>>>3,add,2
#5.0
#>>>3,mul,4
#12.0
#>>>4,div,0
#Division By Zero Is Not Allowed!!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment