Python 小白实例(四) ——体脂率的优化

# 体脂率的计算
# 输入相关数据
#身高
personHeight = input("请输入身高(m)")
personHeight = float(personHeight)
#体重
personWeight = input("请输入体重(kg)")
personWeight = float(personWeight)
#年龄
personAge = input("请输入年龄")
personAge = int(personAge)
#性别
personSex = input("请输入性别(男:1,女:0)")
personSex = int(personSex)
#判断输入的数据是否有效
if not (0 < personHeight < 3 and 0 < personWeight < 300 and 0 < personAge < 150 and (personSex == 0 or personSex == 1)):
    print("您输入的数据存在问题")
    exit()
    # BMI = 体重(kg)/ (身高 * 身高)(M)

else:
    BMI = personWeight / (personHeight * personHeight)
    # 体脂率 = 1.2 * BMI + 0.23 * 年龄 - 5.4 - 10.8 * 性别
    TZL = 1.2 * BMI + 0.23 * personAge - 5.4 - 10.8 * personSex
    #区分男女
    if personSex == 1:
        result = 0.15 < TZL < 0.18
        maxNum = 0.15
        minNum = 0.18
        wenhao = "先生您好"
    elif personSex == 0:
        result = 0.25 < TZL < 0.28
        maxNum = 0.25
        minNum = 0.28
        wenhao = "女士您好"
    if result:
        notice = "恭喜您,您的体脂率正常"
    else:
        if TZL > maxNum:
            notice = "您的体脂率偏高"
        else:
            notice = "您的体脂率偏低"
    print(wenhao + notice)

猜你喜欢

转载自blog.csdn.net/GT_Stone/article/details/80804010
今日推荐