In python, I am working on a program and I feel like this is an easy problem but
ID: 668252 • Letter: I
Question
In python, I am working on a program and I feel like this is an easy problem but I haven't been able to solve it. At the end is a print statement. I wanted to do one for each of the answers, however I keep rinning into syntax errors and type errors.
Any help is appreciated if you can tell me why and show me how it should look. The example I left is the (x+y) in the def main() section.
def gcd(m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
class Fraction:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
def __str__(self):
return str(self.num)+"/"+str(self.den)
def __add__(self,otherfraction):
newnum = self.num*otherfraction.den +
self.den*otherfraction.num
newden = self.den * otherfraction.den
common = gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __eq__(self, other):
firstnum = self.num * other.den
secondnum = other.num * self.den
def __sub__(self, otherfraction):
newnum = self.num * otherfraction.den - self.den * otherfraction.num
newden = self.den * otherfraction.den
common = gcd(newnum,newden)
return Fraction(newnum//common, newden//common)
def __mul__(self, otherfraction):
newnum = self.num * otherfraction.num
newden = self.den * otherfraction.den
common = gcd(newnum,newden)
return Fraction(newnum//common, newden//common)
def __truediv__(self, otherfraction):
newnum = self.num * otherfraction.den
newden = self.den * otherfraction.num
common = gcd(newnum, newden)
return Fraction(newnum//common, newden//common)
return firstnum == secondnum
def main():
x = Fraction(1,2)
y = Fraction(2,3)
print("The sum is:"(x+y))
print(x == y)
print(y - x)
print(x * y)
print(y/x)
Explanation / Answer
class Fraction: def __init__(self, top, bottom): if not isinstance(top, int) or not isinstance(bottom, int): raise TypeError("The data type of numerator or denominator is not int") self.num = top self.den = bottom common = gcd(self.num, self.den) self.num /= common self.den /= common def __str__(self): return str(self.num) + "/" + str(self.den) def __repr__(self): return str(self.num) + "/" + str(self.den) def show(self): print self.num, "/", self.den def getNum(self): return self.num def getDen(self): return self.den def __add__(self, otherfraction): newnum = self.num * otherfraction.den + self.den * otherfraction.num newden = self.den * otherfraction.den return Fraction(newnum, newden) def __eq__(self, other): firstnum = self.num * other.den secondnum = other.num * self.den return firstnum == secondnum def __mul__(self, otherfraction): newnum = self.num * otherfraction.num newden = self.den * otherfraction.den return Fraction(newnum, newden) def __div__(self, otherfraction): newnum = self.num * otherfraction.den newden = self.den * otherfraction.num return Fraction(newnum, newden) def __truediv__(self, otherfraction): newnum = self.num * otherfraction.den newden = self.den * otherfraction.num return Fraction(newnum, newden) def __floordiv__(self, otherfraction): newnum = self.num * otherfraction.den newden = self.den * otherfraction.num #return Fraction(newnum, newden) return newnum // newden def __sub__(self, otherfraction): newnum = self.num * otherfraction.den - self.den * otherfraction.num newden = self.den * otherfraction.den return Fraction(newnum, newden) def __lt__(self, otherfraction): return self.num * otherfraction.den self.den * otherfraction.num def __le__(self, otherfraction): return self.num * otherfraction.den = self.den * otherfraction.num def __ne__(self, otherfraction): return self.num * otherfraction.den != self.den * otherfraction.num def gcd(m, n): while m % n != 0: #oldm = m #oldn = n #m = oldn #n = oldm % oldn m, n = n, m%n return n if __name__ == "__main__": x = Fraction(5,6) y = Fraction(2,3) print x + y print x == y print x * y print x / y print x // y print x - y print x y z = Fraction(1.0, 2)Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.