Python/python 개발

[파이썬/python] 사각형 면적, 둘레 구하기

CE : 하랑 2022. 1. 25. 20:39

 

결과

 

 


 

코드

 


 

복사코드

 

class shape:
    def __init__(self,x,y):
        self.x=x
        self.y=y

    def area(self):
        print("계산할 수 없음!")

    def perimeter(self):
        print("계산할 수 없음!")

class rectangle(shape):
    def __init__(self,x,y,w,h):
        super().__init__(x,y)
        self.w=w
        self.h=h

    def area(self):
        return self.w*self.h

    def perimeter(self):
        return 2*(self.w+self.h)

while(1):
    n=int(input('선택 1)면적 2)둘레 3)종료 : '))

    if n==1:
        w1, h1 = map(int, input('가로, 세로 입력 : ').split())
        r1=rectangle(0,0,w1,h1)
        print("사각형의 면적 : ",r1.area())
        print('\n')
    elif n==2:
        w2, h2 = map(int, input('가로, 세로 입력 : ').split())
        r2=rectangle(0,0,w2,h2)
        print("사각형의 둘레 : ",r2.perimeter())
        print('\n')
    elif n==3:
        print("프로그램 종료")
        break
    else:
        print("다시 선택!\n")