Please help to solve my errors in this Python Program. I wrote a program in Pyth
ID: 3562457 • Letter: P
Question
Please help to solve my errors in this Python Program. I wrote a program in Python and the test def main(): is inserted at the end but I am only passing Excercise #1 and Excercise #2
class Fraction:
def __init__(self,n,d):
if not isinstance(n, int):
print ("Error :: Num is not int")
return
if not isinstance(d, int):
print ("Error :: Den is not int")
return
if d == 0:
print ("Error :: Infinite fraction value ")
return
gc = self.gcd(n,d)
self.num = n//gc
self.den = d//gc
def gcd(self,m,n):
m = abs(m)
n = abs(n)
while(m > 0):
tmp = m
m = n%m
n = tmp
return n
def __str__(self):
return str(self.num) + "/" + str(self.den)
def __add__(self,other):
new_num = self.num * other.den + other.num * self.den
new_den = self.den * other.den
return Fraction(new_num , new_den)
def __sub__(self,other):
new_num = self.num * other.den - other.num * self.den
new_den = self.den * other.den
return Fraction(new_num , new_den)
def __mul__(self,other):
new_num = self.num * other.num
new_den = self.den * other.den
return Fraction(new_num , new_den)
def to_decimal(self):
return float(self.num)/self.den
def __div__(self, other):
new_num = self.num * other.den
new_den = self.den * other.num
common = gcd(new_num,new_den)
return Fraction(new_num//common,new_den//common)
def __gt__(self, other):
return self.to_decimal() > other.to_decimal()
def __ge__(self, other):
return self.to_decimal() >= other.to_decimal()
def __lt__(self, other):
return self.to_decimal() < other.to_decimal()
def __le__(self, other):
return self.to_decimal() <= other.to_decimal()
def __ne__(self, other):
return self.to_decimal() != other.to_decimal()
def getNum(self):
return self.num
def getDen(self):
return self.den
def __eq__(self,other ):
return( self.num==other.num and self.den==other.den)
def main():
x = Fraction(1,2)
y = Fraction(3,4)
print("asserts to test Exercise #1 getNum,getDen")
assert x.getDen() == 2
assert y.getNum() == 3
print("asserts to test Exercise #2 GCD in __init__")
z = Fraction(10,20)
assert z.getNum() == 1 # Num should have been reduced to 1
print(x)
print(y)
print(x+y)
assert x+y == Fraction(5,4) # test original Fraction
print("asserts to test Exercise #3 + - / ")
assert x-y == Fraction(-1,4)
assert x*y == Fraction(3,8)
assert x/y == Fraction(2,3)
print("asserts to test Exercise #4 comparisons" )
assert not (x>y)
assert (x>=x)
assert (y>x)
assert (x<=y)
assert (x<=x)
assert not(x!=x)
assert (x!=y)
assert not (y<=x)
print("asserts to test Exercise #5 integers only" )
print("-- init should print or raise an error from next call" )
Fraction(1.2,3)
main()
Explanation / Answer
class Fraction:
def __init__(self,n,d):
if not isinstance(n, int):
print ("Error :: Num is not int")
return
if not isinstance(d, int):
print ("Error :: Den is not int")
return
if d == 0:
print ("Error :: Infinite fraction value ")
return
gc = self.gcd(n,d)
self.num = n//gc
self.den = d//gc
def gcd(self,m,n):
m = abs(m)
n = abs(n)
while(m > 0):
tmp = m
m = n%m
n = tmp
return n
def __str__(self):
return str(self.num) + "/" + str(self.den)
def __add__(self,other):
new_num = self.num * other.den + other.num * self.den
new_den = self.den * other.den
return Fraction(new_num , new_den)
def __sub__(self,other):
new_num = self.num * other.den - other.num * self.den
new_den = self.den * other.den
return Fraction(new_num , new_den)
def __mul__(self,other):
new_num = self.num * other.num
new_den = self.den * other.den
return Fraction(new_num , new_den)
def to_decimal(self):
return float(self.num)/self.den
def __div__(self, other):
new_num = self.num * other.den
new_den = self.den * other.num
common = self.gcd(new_num,new_den)
return Fraction(new_num//common,new_den//common)
def __gt__(self, other):
return self.to_decimal() > other.to_decimal()
def __ge__(self, other):
return self.to_decimal() >= other.to_decimal()
def __lt__(self, other):
return self.to_decimal() < other.to_decimal()
def __le__(self, other):
return self.to_decimal() <= other.to_decimal()
def __ne__(self, other):
return self.to_decimal() != other.to_decimal()
def getNum(self):
return self.num
def getDen(self):
return self.den
def __eq__(self,other ):
return( self.num==other.num and self.den==other.den)
def main():
x = Fraction(1,2)
y = Fraction(3,4)
print("asserts to test Exercise #1 getNum,getDen")
assert x.getDen() == 2
assert y.getNum() == 3
print("asserts to test Exercise #2 GCD in __init__")
z = Fraction(10,20)
assert z.getNum() == 1 # Num should have been reduced to 1
print(x)
print(y)
print(x+y)
assert x+y == Fraction(5,4) # test original Fraction
print("asserts to test Exercise #3 + - / ")
assert x-y == Fraction(-1,4)
assert x*y == Fraction(3,8)
assert x/y == Fraction(2,3)
print("asserts to test Exercise #4 comparisons" )
assert not (x>y)
assert (x>=x)
assert (y>x)
assert (x<=y)
assert (x<=x)
assert not(x!=x)
assert (x!=y)
assert not (y<=x)
print("asserts to test Exercise #5 integers only" )
print("-- init should print or raise an error from next call" )
Fraction(1.2,3)
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.