-3

Is there any way to make area() refer to check()? Currently, codes of check() is the same in area().

class Rectangle:
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.x2 = x2
        self.y1 = y1
        self.y2 = y2

    def check(self): #Checking this rectangle 
        if abs(self.x1-self.x2) == 0 or abs(self.y1-self.y2)==0:
            return False
        else :
            return True

    def area(self): #Calculating width
        if abs(self.x1-self.x2) == 0 or abs(self.y1-self.y2)==0:
            return False
        else : 
            area = abs(self.x1-self.x2)*abs(self.y1-self.y2)
            return area
신은화
  • 3
  • 3

1 Answers1

0

In your check(self) method, you don't need any if nor abs(). You can just do:

def check(self):
    return self.x1 != self.x2 and self.y1 != self.y2

About the area(self) method, you could just do:

def area(self):
    if not self.check():
        return False
    else:
        return abs(self.x1-self.x2)*abs(self.y1-self.y2)

Now your code should be cleaner.

iohsfush
  • 107
  • 8