You are viewing our Forum Archives. To view or take place in current topics click here.
PYTHON HELP! - Can someone help me?
Posted:

PYTHON HELP! - Can someone help me?Posted:

xLinz
  • Christmas!
Status: Offline
Joined: Jul 12, 20158Year Member
Posts: 103
Reputation Power: 15
Status: Offline
Joined: Jul 12, 20158Year Member
Posts: 103
Reputation Power: 15
I have a lab due for computer science tonight. This is python 3, I just need someone to check over it and double check it please, thank you.



class Rational(object):

    def __init__(self, num = 0, den = 1):

        self.num = num
        self.den = den

    #------------------------------------------------------------------

    def __add__(self, other):

        add1 = self.num * other.den
        add2 = self.den * other.num
        top = add1 + add2
        bottom = self.den * other.den
        return Rational(top, bottom)

    #------------------------------------------------------------------

    def __sub__(self, other):

        sub1 = self.num * other.den
        sub2 = self.den * other.num
        top = sub1 - sub2
        bottom = self.den * other.den
        return Rational(top, bottom)

    #------------------------------------------------------------------

    def __mul__(self, other):

        mul1 = self.num * other.num
        mul2 = self.den * other.den
        return Rational(mul1, mul2)

    #------------------------------------------------------------------

    def __truediv__(self, other):

        div1 = self.num * other.den
        div2 = self.den * other.num
        return Rational(div1, div2)

    #------------------------------------------------------------------

    def __str__(self):

        return str(self.num) + '/' + str(self.den)

    #------------------------------------------------------------------

    def __repr__(self):

        return self.__str__()

    #------------------------------------------------------------------

    def __le__(self, other):

        le1 = self.num, self.den
        le2 = other.num, other.den

        if le1 <= le2:
            return True
        else:
            return False

    #------------------------------------------------------------------

    def __ge__(self, other):

        ge1 = self.num, self.den
        ge2 = other.num, other.den

        if ge1 >= ge2:
            return True
        else:
            return False

    #------------------------------------------------------------------

    def __gt__(self, other):

        gt1 = self.num, self.den
        gt2 =other.num, other.den

        if gt1 > gt2:
            return True
        else:
            return False

    #------------------------------------------------------------------

    def __lt__(self, other):

        lt1 = self.num, self.den
        lt2 = other.num, other.den

        if lt1 < lt2:
            return True
        else:
            return False

    #------------------------------------------------------------------

    def __ne__(self, other):

        nequ1 = self.num * other.den
        nequ2 = self.den * other.num

        if nequ1 != nequ2:
            return True
        else:
            return False

    #------------------------------------------------------------------

    def __eq__(self, other):

        equ1 = self.num * other.den
        equ2 = self.den * other.num

        if equ1 == equ2:
            return True
        else:
            return False

#----------------------------------------------------------------------

def main():

#----------------------------------------------------------------------

    if __name__ == '__main__': main()
#2. Posted:
speed
  • Winter 2023
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Nobody is going to check over it if we have no idea what it's supposed to do.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.