Как сделать так чтобы можно было проводить операции не только с дробями но и целого числа с дробью. Сейчас выдает ошибку, недостаток атрибутов.
Код:
class Fraction:
def __init__(self, a, b):
self.a = a
self.b = b
def __str__(self):
return str(self.a) + "/" + str(self.b)
def show(self):
print(self.a, '/', self.b, sep='')
def __add__(self, otherfraction):
newnum = self.a * otherfraction.b + self.b * otherfraction.a
newden = self.b * otherfraction.b
print(str(newnum) + '/' + str(newden))
return Fraction(newnum, newden)
def __sub__(self, otherfraction):
newnum = self.a * otherfraction.b - self.b * otherfraction.a
newden = self.b * otherfraction.b
print(str(newnum) + '/' + str(newden))
return Fraction(newnum, newden)
def __mul__(self, otherfraction):
newnum = self.a * otherfraction.a
newden = self.b * otherfraction.b
print(str(newnum) + '/' + str(newden))
return Fraction(newnum, newden)
def __truediv__(self, otherfraction):
newnum = self.a * otherfraction.b
newden = self.b * otherfraction.a
print(str(newnum) + '/' + str(newden))
return Fraction(newnum, newden)
class OperationsOnFraction(Fraction):
def getint(self):
integer = self.a // self.b
print(integer)
return integer
def getfloat(self):
float_number = self.a / self.b
print(float_number)
return float_number
first_fraction = Fraction(1, 2)
second_fraction = Fraction(3, 4)
Fraction.__add__(first_fraction, second_fraction)
Fraction.__sub__(first_fraction, second_fraction)
Fraction.__mul__(first_fraction, second_fraction)
Fraction.__truediv__(first_fraction, second_fraction)
x = OperationsOnFraction(2, 3)
x.getint()
x.getfloat()