判断一个人的体重是肥胖还是正常
BMI计算体脂率方法
图片:
代码如下:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
while True:
height= input("输入身高")
weight =input("输入体重")
age = input("输入年龄")
sex = input("输入性别")
try:
for i in [height,weight,age,sex]:
if i==height:
if type(float(i)) == float and 0<float(i)<3:
height = float(height)
else:
print("你输入的身高有误,清重新输入")
height= input("输入身高")
height = float(height)
if i==weight:
if (type(float(i))==float) and 0< float(i)<300:
weight=float(weight)
else:
print("你输入的体重有误,清重新输入")
weight =input("输入体重")
weight=float(weight)
if i==age:
if type(int(i)==int) and 0 < int(i)< 100:
age = int(age)
else:
print("你输入的年龄有误,清重新输入")
age = input("输入年龄")
age = int(age)
if i==sex:
if i=="男":
sex=1
elif i=="女":
sex=0
else:
print("你输入的性别有误,清重新输入")
sex = input("输入性别")
break
except:
print("TypeError")
BMI =weight/(height*height)
fat= 1.2*BMI+0.23*float(age)-5.4-10.8*float(sex)
if sex==1:
if fat>=0.15 and fat<=0.18:
print("体重正常")
elif fat<0.15:
print("偏瘦")
else:
print("肥胖")
else:
if fat>=0.25 and fat<=0.28:
print("体重正常")
elif fat<0.25:
print("偏瘦")
else:
print("肥胖")
本篇文档是原创,转载请注明出处,谢谢。