面向对象——计算身体质量的指数

class BMI:
    
    def __init__(self,name,age,weight,high):
        self.name = name
        self.age = age
        self.weight = weight
        self.high = high
    def say_hello(self):
        self.jieguo=self.weight/self.high/self.high
        print("{n}的BMI值为{m}".format(n=self.name,m=self.jieguo))
        m = self.jieguo
        if m < 18.5 : 
            print ("偏瘦")
        elif m < 24 :
            print ("正常")
        elif m < 30 :
            print ("偏胖")
        elif m > 30:
            print ("肥胖")
        
#实例化
zhangsan = BMI("zhangsan","18",70,1.75)
# 调用方法
zhangsan.say_hello()

zhangsan的BMI值为22.857142857142858
正常

上一篇:第一天 使用pycharm编写python记录每一天


下一篇:Python中用class演示BMI指数