The code below is supposed to supposed to print the answers to different fractio
ID: 667656 • Letter: T
Question
The code below is supposed to supposed to print the answers to different fraction calculations. However when I run it, it prints out nothing. If it is possible please revise the code to make it work as it should. Thank you!
Original code:
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 show(self):
print (self.num),"/",(self.den)
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
return firstnum == secondnum
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)
def __lt__(self, otherfraction):
num1 = self.num * otherfraction.den
num2 = otherfraction.num * otherfraction.num
if num1 < num2:
return True
else:
return False
def main():
x = Fraction(1,2)
y = Fraction(2,3)
print (x + y)
print (x == y)
print (y - x)
print (x * y)
print (y/x)
print (x < y)
Explanation / Answer
#! /usr/bin/python class Fraction: def __init__(self,top,bottom): if isinstance(top,int) and isinstance(bottom,int): common = gcd(top,bottom) self.num = top//common self.den = bottom//common else: raise RuntimeError("Numerator or denominator is not a valid integer") def __str__(self): return str(self.num)+'/'+str(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 __sub__(self,otherfraction): newnum = self.num * otherfraction.den - self.den * otherfraction.num newden = self.den * otherfraction.den return Fraction(newnum,newden) def __mul__(self,otherfraction): newnum = self.num * otherfraction.num newden = self.den * otherfraction.den return Fraction(newnum,newden) def __truediv__(self,otherfraction): newnum = self.num * otherfraction.den newden = self.den * otherfraction.num return Fraction(newnum,newden) def __eq__(self,otherfraction): firstnum = self.num * otherfraction.den secondnum = self.den * otherfraction.num return (firstnum == secondnum) def __ne__(self,otherfraction): firstnum = self.num * otherfraction.den secondnum = self.den * otherfraction.num return (firstnum != secondnum) def __lt__(self,otherfraction): firstnum = self.num * otherfraction.den secondnum = self.den * otherfraction.num return (firstnum = secondnum) def gcd(m,n): while m%n != 0: oldm = m oldn = n m = oldn n = oldm%oldn return nRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.