Answer the question
In order to leave comments, you need to log in
Fraction class: how to add integer operations?
Good afternoon! I wrote a code to work with fractions:
class Fraction:
def __init__(self, a, b):
self.a = a
self.b = b
def __str__(self):
return '{}/{}'.format(self.a, self.b)
def __add__(self, other):
return Fraction(self.a + other.a, self.b + other.b)
def __sub__(self, other):
return Fraction(self.a - other.a, self.b - other.b)
def __mul__(self, other):
return Fraction(self.a * other.a, self.b * other.b)
It is assumed that the operation of addition / subtraction / multiplication can be carried out both only between fractions, and between a fraction and an integer.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question