python - unbound method must be called with instance as first argument -
i trying build simple fraction calculator in python2.x
from fractions import fraction class thefraction: def __init__(self,a,b): self.a = self.b =b def add(self): return a+b def subtract(self,a,b): return a-b def divide(self,a,b): return a/b def multiply(self,a,b): return a/b if __name__=='__main__': try: = fraction(input('please type first fraction ')) b = fraction(input('please type second fraction ')) choice = int(input('please select 1 of these 1. add 2. subtract 3. divide 4. multiply ')) if choice ==1: print(thefraction.add(a,b)) elif choice==2: print(thefraction.subtract(a,b)) elif choice==3: print(thefraction.divide(a,b)) elif choice==4: print(thefraction.multiply(a,b)) except valueerror: print('value error!!!!!')
i not sure made correct class can instantiated, used like,thefraction.add
in side of __name__=='__main__'
. did miss something?
it's meant done this:
thefraction = thefraction(a, b) if choice == 1: print(thefraction.add())
then in class:
def add(self): return self.a + self.b
and on. don't include a
, b
parameters in methods.
and yes, go through tutorial on classes again. thoroughly.
Comments
Post a Comment