Search
See the general homework guidelines!
You are going to write a simple class to represent fractions in Python. Place it in the module called fraction.py Your class should:
fraction.py
def __init__(self, numerator, denominator): self.numerator = numerator # top self.denominator = denominator # bottom
ZeroDivisionError
__str__()
print()
# the output should be in the form: numerator/denominator f1 = Fraction(2,3) print(f1) >>> 2/3 # a reduced fraction should be printed - a fraction in which the numerator and denominator are integers that have no other common divisors than 1. f2 = Fraction(12,18) print(f2) >>> 2/3 # print only numerator in case the denominator is 1 f3 = Fraction(8,2) print(f3) >>> 4 # print initial ''-'' in case the fraction is negative. No other representations of negative fractions will be accepted. f4 = Fraction(-2,3) print(f4) >>> -2/3 f5 = Fraction(2,-3) print(f5) >>> -2/3 f6 = Fraction(-2,-3) print(f6) >>> 2/3
__add__()
__sub__()
__mul__()
__truediv__()
f1 = Fraction(12,18) f2 = Fraction(4,9) print("f1 = " + str(f1)) print("f2 = " + str(f2)) print("f1 + f2 = " + str(f1+f2)) print("f1 - f2 = " + str(f1 - f2)) print("f1 * f2 = " + str(f1 * f2)) print("f1 / f2 = " + str(f1 / f2)) >>> f1 = 2/3 >>> f2 = 4/9 >>> f1 + f2 = 10/9 >>> f1 - f2 = 2/9 >>> f1 * f2 = 8/27 >>> f1 / f2 = 3/2
__eq__()
__lt__()
__le__()
__gt__()
__ge__()