判斷一個人的體重是肥胖還是正常
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("肥胖")
本篇文檔是原創,轉載請注明出處,謝謝。