2021年1月28日星期四

'int' object has no attribute 'den' error message in Python

def gcd(m, n):      while m % n != 0:          m, n = n, m % n      return n      class Fraction:      def __init__(self, top, bottom):          if not isinstance(top, int):              raise ValueError("Numenator should be an integer.")          if not isinstance(bottom, int):              raise ValueError("Denomenator should be an integer.")          common = gcd(top, bottom)          self.num = top // common          self.den = bottom // common        def __str__(self):          return "{:d}/{:d}".format(self.num, self.den)        def __eq__(self, other_fraction):          first_num = self.num * other_fraction.den          second_num = other_fraction.num * self.den          return first_num == second_num                          def __add__(self, other_fraction):          new_num = self.num * other_fraction.den \                    + self.den * other_fraction.num          new_den = self.den * other_fraction.den          return Fraction(new_num, new_den)      #__radd__ is only called if the left object does not have an __add__ method, or that method does not know how to add the two objects.      def __radd__(self, other_fraction):          new_num = self.num * other_fraction.den \                    + self.den * other_fraction.num          new_den = self.den * other_fraction.den          return Fraction(new_num, new_den)             def show(self):          print("{:d}/{:d}".format(self.num, self.den))        def get_num(self):          return self.num        def get_den(self):          return self.den            def __sub__(self, other_fraction):          new_num = self.num * other_fraction.den \                    - self.den * other_fraction.num          new_den = self.den * other_fraction.den          return Fraction(new_num, new_den)            def __mul__ (self, other_fraction):          new_num = self.num * other_fraction.num          new_den = self.den * other_fraction.den          return Fraction(new_num, new_den)            def __truediv__(self, other_fraction):          new_num = self.num * other_fraction.den          new_den = self.den * other_fraction.num          return Fraction(new_num, new_den)            def __gt__(self,other_fraction):          return self.num*other_fraction.den>self.den*other_fraction.num            def __ge__(self,other_fraction):          return self.num*other_fraction.den>=self.den*other_fraction.num            def __lt__(self,other_fraction):          return self.num*other_fraction.den<self.den*other_fraction.num            def __le__(self,other_fraction):          return self.num*other_fraction.den<=self.den*other_fraction.num            def __ne__(self,other_fraction):          return self.num!=other_fraction.num and self.den!=other_fraction.num  

So this is what I am doing now and when I run this code with

print(x + 1)  print(1 + x)  

these two, I get 'int' object has no attribute 'den' this error message.

What am I doing wrong?

https://stackoverflow.com/questions/65948529/int-object-has-no-attribute-den-error-message-in-python January 29, 2021 at 11:50AM

没有评论:

发表评论