0
from fraction.py import *

f1 = Fraction(3,4)
f2 = Fraction(2,3)
f3 = f1 * f2
print(f3)
File "python.4", line 1, in <module>
   from fraction import *
  File "/home/PYTHON/examples/fraction.py", line 50, in <module>
    f3= f1 * f2
TypeError: unsupported operand type(s) for *: 'Fraction' and 'Fraction'

There is a TypeError but I'm not sure how to correct it. I am attempting to generate a fraction. I have defined the numerator and denominator. I have the code saved on a separate file and I'm attempting to import it into the current file to apply it to this example.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Victoria
  • 17
  • 4

1 Answers1

0

I do not know the content of the "fraction.py" file but my guess is you did not return the supported type for operand * (the common supported type would be Integer, Float...).

My suggestion is that in your custom class, you should override the magic methods __mul__(self, other).

R.Squallo
  • 16
  • 2
  • def __mul__(self, other): den = self.den * other.num num = self.num * other.den return (Fraction(self.num * other.num, self.den * other.den)) f1 = Fraction(3,4) f2 = Fraction(2,3) print(f1 * f2) – Victoria Jul 19 '22 at 02:54