Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unbound method must be called with instance as first argument

I am trying to build simple fraction calculator in python2.x

from fractions import Fraction
class Thefraction:

    def __init__(self,a,b):
        self.a = 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:
        a = Fraction(input('Please type first fraction '))
        b = Fraction(input('Please type second fraction '))
        choice = int(input('Please select one 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 am not sure that I made correct class that can be instantiated, however I used it like,Thefraction.add in side of __name__=='__main__'. Did I miss something?

like image 604
jayko03 Avatar asked Jan 26 '26 07:01

jayko03


2 Answers

It's meant to be done like this:

thefraction = Thefraction(a, b)
if choice == 1:
    print(thefraction.add())

Then in your class:

def add(self):
    return self.a + self.b

and so on. Don't include a and b as parameters in the methods.

And yes, go through a tutorial on classes again. Thoroughly.

like image 159
Alex Hall Avatar answered Jan 28 '26 22:01

Alex Hall


You haven't put () in front of your theFraction object. Even if you did, you will be faced by another calamity..You initialize your object with two variables (a,b) which means you will to call your object like

Thefraction(a,b).add(a,b)

I dont think you want this because a and b are local variables in each methods.. which is kind of making variables that you do not need. What I am assuming you would like to have is this.

  Thefraction(a,b).add()

here is the full code

from fractions import Fraction
class Thefraction:

    def __init__(self,a,b):
        self.a = a
        self.b =b
    def add(self):
        return self.a+ self.b
    def subtract(self):
        return self.a-self.b
    def divide(self):
        return self.a/self.b
    def multiply(self):
        return self.a/self.b

if __name__=='__main__':
    try:
        a = Fraction(input('Please type first fraction '))
        b = Fraction(input('Please type second fraction '))
        choice = int(input('Please select one of these 1. add 2. subtract 3. divide 4. multiply '))
        if choice ==1:
            print(Thefraction(a,b).add())
        elif choice==2:
            print(Thefraction(a,b).subtract())
        elif choice==3:
            print(Thefraction(a,b).divide())
        elif choice==4:
            print(Thefraction(a,b).multiply())
    except ValueError:
        print('Value error!!!!!')
like image 38
repzero Avatar answered Jan 28 '26 20:01

repzero