lundi 13 juin 2016

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?


Aucun commentaire:

Enregistrer un commentaire