Skip to content

Instantly share code, notes, and snippets.

@johnhonan
Created November 29, 2017 20:26
Show Gist options
  • Save johnhonan/f5df88632e302f8da21f5c690137a10f to your computer and use it in GitHub Desktop.
Save johnhonan/f5df88632e302f8da21f5c690137a10f to your computer and use it in GitHub Desktop.
python calculator
class Calculator(object): # define a calculator object
def add(self, x, y): # define addition function
number_types = (int, long, float, complex) # accepted number types
if isinstance(x, number_types) and isinstance(y, number_types): # if both values are accepted number types
return x + y # return the sum of the values
else:
raise ValueError # if one or both values are not accepted number types return a Value Error
def subtract(self, x, y):
number_types = (int, long, float, complex)
if isinstance(x, number_types) and isinstance(y, number_types):
return x - y
else:
raise ValueError
def multiply(self, x, y):
number_types = (int, long, float, complex)
if isinstance(x, number_types) and isinstance(y, number_types):
return x * y
else:
raise ValueError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment