본문 바로가기
Python/개념익히기

BMI 프로그램 클래스로 만들기

by ADELA_J 2023. 6. 14.
class Bmi:
    def __init__(self, height, weight):
        self.height = height
        self.weight = weight

    def cal(self):
        return self.weight/ self.height **2

class BmiOutput(Bmi):
    def __init__(self, name, height, weight):
        super().__init__(height, weight)
        self.name = name

    def get_bmi_kind(self):
        if self.cal() < 18.5:
            return "저체중 😂"
        elif self.cal() < 22.9 and self.cal() > 18.5:
            return "정상 👍"
        elif self.cal() < 24.9 and self.cal() > 23:
            return "위험체중 🤔"
        elif self.cal() < 29.9 and self.cal() > 25:
            return "1단계 비만😵"
        else :
            return "2단계 비만🐖"

    def get_bmi_result(self):
        result = "{}님은 키 {}cm, 몸무게 {}kg으로 BMI 지수는 {:.1f}입니다. 따라서 {}으로 분류됩니다.".format(self.name, self.height, self.weight, self.cal(), self.get_bmi_kind())
        return result

person1 = BmiOutput("냠냠이", 1.65, 65)
print(person1.get_bmi_result())
person2 = BmiOutput("냥냥이", 1.80, 90)
print(person2.get_bmi_result())