#!/usr/bin/python # -*- coding: utf-8 -*- #小明身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方) #帮小明计算他的BMI指数,并根据BMI指数: #低于18.5:过轻 # 18.5-25:正常 # 25-28:过重 # 28-32:肥胖 # 高于32:严重肥胖
import logging
def lgq(height,weight): bmi =weight/height bmi = bmi / height
return bmi
def shengao(height): height = input('欢迎使用我司产品,请输入身高m: ') if height.strip()=='': print('请重新输入您的身高') return shengao(height) height = float(height) if height <= 0: print('身高数据要大于0哦') return shengao(height) elif height>3: print('您输入的数据不能大于3哦') return shengao(height)
else: return height def tizhong(weight): weight = input('请输入体重kg: ') if weight.strip()=='': print('请重新输入您的体重') return tizhong(weight) weight = float(weight) if weight <= 0: print('体重数据要大于0') return tizhong(weight) else: return weight
print('•低于18.5:过轻 •18.5-25:正常 •25-28:过重 •28-32:肥胖 •高于32:严重肥胖 ') bmi =0 height = 0 weight = 0 try:
height = shengao(height)
weight = tizhong(weight)
except ValueError as e: print('您输入的数据有误,请输入大于0的数字')
try: bmi = lgq(height,weight) except ZeroDivisionError as e: print('身高不能输入数据为0') if bmi>18.5 and bmi<=25: print('您的bmi指数为%.2f' % bmi,'您的体重正常') elif bmi>25 and bmi<=28: print('您的bmi指数为%.2f' % bmi,'您的体重过重') elif bmi>28 and bmi<=32: print('您的bmi指数为%.2f' % bmi,'您的体重肥胖') elif bmi>32: print('您的bmi指数为%.2f' % bmi,'你的体重严重肥胖') else: print('您的bmi指数为%.2f' % bmi,'您的体重太瘦了')