Python中用class演示BMI指数

class BMI:
    def __init__(self,xingming,nianling,tizhong,shengao):
        self.xingming = xingming
        self.nianling = nianling
        self.tizhong = tizhong
        self.shengao = shengao
        self.bmi = tizhong/(shengao*shengao)
        t = self.bmi
        if t < 18.5:
            n = "偏瘦"
        elif t >= 18.5 and t < 24:
            n = "正常"
        elif t >= 24 and t < 30:
            n = "偏胖"
        self.jiankang = n
    def the_BMI(self):
        print("{n}".format(n=self.xingming),"的BMI是:{n}".format(n=self.bmi),"他/她的健康状况是:{n}".format(n=self.jiankang))
    def the_age(self):
        print("{n}".format(n=self.xingming),"的年龄是:{n}".format(n=self.nianling)) 
    def the_weighht(self):
        print("{n}".format(n=self.xingming),"的体重是:{n}".format(n=self.tizhong)) 
    def the_high(self):
        print("{n}".format(n=self.xingming),"的身高是:{n}".format(n=self.shengao))   
zhangsan = BMI("zhangsan", "20", 140,175)`

`zhangsan.the_BMI()
上一篇:面向对象——计算身体质量的指数


下一篇:Python入门-04.Python3注释缩进规则(包含快捷键)