有人可以帮助我__gt__并帮助我使用__radd__吗?

时间:2015-07-21 10:30:28

标签: python python-3.x

我不确定我的__gt__出了什么问题,但我得不到正确的结果。我试过改变一些东西,但我并不总是得到正确的打印。另外,我真的不了解__radd__或如何实现它。当我得到我的打印时,另一个快速的问题有时我会得到像2 0/6这样的答案我怎么能得到它才能打印出2?

这是我的代码:

class Fraction:
    def __init__(self,top,bottom):
        self.num = top
        self.den = bottom
        self.gcd = gcd(self.num, self.den)

    def __str__(self):
        if self.den == 0:
            return str(0)
        elif self.num >= self.den:
            if self.den == 1:
                return str(self.num)
            else:
                return str(self.num // self.den)+\
                   ' '+str(self.num%self.den)+\
                   '/'+str(self.den)
        else:
            return str(self.num)+"/"+str(self.den)

    def show(self):
        print(self.num,"/",self.den)

    def __add__(self,otherfraction):
        newnum = self.num*otherfraction.den + \
                     self.den*otherfraction.num
        newden = self.den * otherfraction.den
        common = self.gcd
        return Fraction(newnum//common,newden//common)

    def __sub__(self,otherfraction):
        if self.den == 1:
            sub = self.num - otherfraction.num
            return sub
        else:
            newnum = self.num*otherfraction.den - \
                         self.den*otherfraction.num
            newden = self.den * otherfraction.den
            common = self.gcd
            return Fraction(newnum//common,newden//common)

    def __mul__(self,otherfraction):
        newnum = self.num*otherfraction.num 
        newden = self.den * otherfraction.den

        return Fraction(newnum//newnum,newden//newnum)

    def __truediv__(self,otherfraction):
        newnum = self.num*otherfraction.den 
        newden = self.den * otherfraction.num
        common = self.gcd
        return Fraction(newnum//common,newden//common)

    def __gt__(self,other):
        if self.den == 1:
            if self.num > other.num:
                return self.num
        else:
            frac1 = self.num*other.den 
            frac2 = self.den * other.num
            if frac1 > frac2:
                return self.num//self.den
            else:
                return other.num//other.den
    def __radd__(self, other):
        if other == 0:
            return self
        else:
            return self.__add__(other)

    def __eq__(self, other):
        firstnum = self.num * other.den
        secondnum = other.num * self.den

        return firstnum == secondnum

def gcd(m,n):
    while m%n != 0:
        oldm = m
        oldn = n

        m = oldn
        n = oldm%oldn
    return n


def main():

        getNum1 = int(input("Enter a numerator 1: "))
        getDen1 = int(input("Enter a denominator 1: "))

        getNum2 = int(input("Enter a numerator 2: "))
        getDen2 = int(input("Enter a denominator 2: "))

        f1 = Fraction(getNum1,getDen1)
        f2 = Fraction(getNum2,getDen2)

        print("[",f1,"]","[",f2,"]",sep='')

        f3 = f1 + f2
        print("Adding Fractions:",f3)
        f3 = f1 - f2
        print("Subtracting Fraction:",f3)

        f3 = f1 * f2

        print("Multiply Fraction:",f3)

        f3 = f1 / f2
        print("Dividing Fraction:",f3)

        if f1 > f2:
            print(f1,"Greater than",f2)
        else:
            print(f2,"Greater than",f1)

        if f1 == f2:
            print("Fractions are equal")
        else:
            print("Fractions are not equal")



main()

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

来自评论 -

  

是的,我只是想让它返回自我,如果是真的或返回其他,取决于哪个分数大于另一个。

似乎你误解了__gt__()的工作原理,当self大于另一个时,这个函数应该返回'True'(或者像true一样真值),它应该返回False(或者当self小于other时,像false一样虚假,等等。让我们举个例子(假设__gt__()当self更大时返回self的值,否则返回other的值),当你这样做时 -

if f1 > f2:

这将调用__gt__()函数,该函数将返回self的值或其他值,它们都是true个值,除非它们返回0,假设后者不是这种情况。这总是会导致f1被认为更大,因为您返回的值(具有True类似值)而不是True或'False`。

您应该返回类似的内容 -

def __gt__(self,other):
    if self.num/self.den > other.num/other.den:
        return True
    else:
        return False

还关于radd -

__radd__是反向添加。当Python尝试评估x + y时,它首先尝试调用x.__add__(y)。如果此操作失败,则会回退到y.__radd__(x)

在评估sum()对象列表时非常有用,我相信sum()中的第一个电话实际上是对__radd__()的调用,你可以查看{{3} }。

__radd__()出现在哪里的例子,假设你正在做 -

>>> 0 + f1 # where f1 is your object.

这最终会调用f1.__radd__(0)

这是sum()的情况,因为sum()0的初始值开头,并开始逐一将列表中的元素添加到其中。