2015-07-21 85 views
-4

我不知道我在做什麼錯我的__gt__但我沒有得到正確的結果。我嘗試過改變事物,但我並不總是得到正確的打印輸出。另外,我不太瞭解__radd__或如何實施它。另外一個快速的問題是,當我打印出來的時候,有時我會得到像2 0/6這樣的答案,我怎麼才能打印出2呢?有人可以用我的__gt__幫助我,並幫助我使用__radd__?

這裏是我的代碼:

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() 

預先感謝您的幫助!

+0

你想用'__gt __()'方法實現什麼?當'self'大於'other'時,你想返回true嗎? –

+0

是的,我只是試圖讓它返回自我,如果真或返回其他取決於哪一部分比另一部分大。 – keggy

回答

0

從評論 -

是的,我只是想,如果有真正還給自己或其他回報取決於其分數比其他更大。

好像你是誤解如何__gt__()的作品,這個功能應該返回「真」(或真像值)時,自比其他的大,它應該返回False(或假像值,如0等),當self小於other。讓我們把你的例子(assumming __gt__()回報價值的self「當自我是更大的,否則other價值」),當你做 -

if f1 > f2: 

這將調用__gt__()函數將返回要麼自我的價值或其他的值,它們都是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__()一個電話,你可以看看這個here。哪裏__radd__()進入圖片

例子,假設你正在做的 -

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

這將最終調用f1.__radd__(0)

這是sum()的情況,因爲sum()以初始值0開始,並開始逐個向列表中添加元素。

+0

好的,謝謝,這對於_____有意義,但我仍然不明白我將如何use_radd__這個程序,因爲我不認爲評估sum()。對不起,這只是讓我感到困惑。 – keggy

+0

請檢查我的最新答案,如果它仍不清楚,請告訴我。 –

+0

好吧,有點有道理,但我只是不明白我想在我的程序中總結(),就像我沒有看到我假設在程序中使用這個對不起,因爲感到疼痛 – keggy