Created
November 29, 2017 20:26
-
-
Save johnhonan/f5df88632e302f8da21f5c690137a10f to your computer and use it in GitHub Desktop.
python calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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