Python 有人能帮我做我的uu gt uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?
我不确定我的uu gt_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。我尝试过改变一些事情,但我并不总能得到正确的打印结果。另外,我也不太理解radd或者如何实现它。还有一个快速的问题,当我打印出来的时候,有时我会得到20/6的答案,我怎么能把它打印出来呢 这是我的密码:Python 有人能帮我做我的uu gt uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?,python,python-3.x,Python,Python 3.x,我不确定我的uu gt_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。我尝试过改变一些事情,但我并不总能得到正确的打印结果。另外,我也不太理解radd或者如何实现它。还有一个快速的问题,当我打印出来的时候,有时我会得到20/6的答案,我怎么能把它打印出来呢 这是我的密码: class Fraction: def __init__(self,top,bottom): self.n
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()
提前感谢您的帮助 从评论中-
是的,我只是想让它返回self,如果是真的,或者返回other,这取决于哪个分数大于另一个分数 似乎您误解了_gt _uuu的工作原理,当self大于另一个时,此函数应返回'True'或类True值,当self小于另一个时,它应返回False或类False值,如0等。让我们以您的示例为例,假设当self较大时,summing _gt _;返回self的值,否则返回其他值-
if f1 > f2:
这将调用_gt__函数,该函数将返回self的值或other的值,这两个值都是true-like值,除非它们返回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_uu是反向加法。当Python尝试计算x+y时,它首先尝试调用x.。\uuuu add\uuuu y。如果失败,则返回到y.uu radd_uux
这在计算对象列表上的sum时非常有用,我相信sum中的第一个调用实际上是对uu radd_uu的调用,您可以检查一下
比如说,你正在做什么-
>>> 0 + f1 # where f1 is your object.
这将最终调用f1
sum就是这种情况,因为sum从初始值0开始,然后开始逐个向其中添加列表中的元素。您试图用_gt_uuu方法实现什么?当self大于other时,你想返回true吗?是的,我只是想让它返回self,如果true,或者返回other,这取决于哪个分数大于other。好的,谢谢,这很有意义,但是我仍然不明白我会如何使用这个程序,因为我不认为计算sum。很抱歉,这个让我很困惑。请检查我的最新答案,如果还不清楚,请告诉我。好吧,这有点道理,但我不明白我在我的程序中尝试求和,就像我不知道我应该如何在程序中使用它一样抱歉,我是一个pain@keggy看起来你理解了你实现它的方式。您依赖于0+self==self和其他+self==self+other,当self是一个有理数时,这很好。Howewer你的uuu add_uuuuu的实现不能处理other不是一个有理数的情况,这取决于你的目的,也可能是不正确的。所以我的方法确实有效,只要other是一个有理数,对吗@天王