I started to learn Python 2 weeks ago, and I was doing an exercise about class (which consists of creating my own "Fraction" class) when I got the error in the title. Why ? And how to solve it ? Sorry, i'm not the type of person who often ask for help, but now I really don't know how to solve it. It seems that the error occurs only in "str" method.
Here you have my code(idk if this will appear properly..):
from math import gcd
class Fraction :
def __init__(self,a,b):
self.num = a
self.den = b
def __str__(self):
return str(self.num)+" / "+str(self.den)
def reduire(self):
return self.num//gcd(self.num,self.den) , self.den//gcd(self.num,self.den)
frac = Fraction(30,40)
print(Fraction.__str__(Fraction.reduire(frac)))
reduirereturns a tuple, and then you try to get thenumfield, and tuples don't have named fields. You probably wantreduireto return aFraction.