Реализовал класс Fraction для операций с дробями.
def gcd(a, b):
while a % b != 0:
old_a = a
old_b = b
a = old_b
b = old_a % old_b
return b
class Fraction:
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return '{}/{}'.format(self.a, self.b)
def __add__(self, other):
if not isinstance(self, Fraction):
return NotImplemented
new_a = self.a * other.b + \
self.b * other.a
new_b = self.b * other.b
common = gcd(new_a, new_b)
return Fraction(new_a // common, new_b // common)
Input: print(Fraction(10, 5) + Fraction(10, 5))
Output: 4/1
Input: print(Fraction(10, 5) + 1)
Output: AttributeError: 'int' object has no attribute 'b'
Вопрос: как сложить дробь с целым числом?