Python自学——if嵌套 案例一的优化

#输入
#加上容错处理,数据有效性的处理
length = float(input('请输入您的身高(m):'))
weight = float(input('请输入您的体重(kg):'))
age = int(input('请输入您的年龄:'))
sex_input = input('请输入您的性别:')
if not(0<length<3 and 0<weight<300 and 0<age<100 and (sex_input == '男' or sex_input == '女')):
    exit()
#数据处理
sex = () 
if sex_input=='男': 
    sex = 1
elif sex_input=='女':
    sex = 0
print (sex)
BMI = weight/length/length
score = 1.2*BMI + 0.23*age -5.4 -18.8*sex
print(BMI)
print(score) 
# if sex_input=='男': 
#     mim = 15.00
#     max = 18.00
#     print('先生你好')
# elif sex_input=='女':
#     #这里是elif,不是else
#     mim = 25.00
#     max = 28.00
#     print('女士你好')
# if mim
#     print('恭喜你,你的身体非常健康')
# elif score
#     print('请注意,你的身体偏瘦')
# elif score>max:
#     print('请注意,你的身体偏胖')
#下面这几行可以简化如下(把print放在最后一起输出)
if sex_input=='男': 
    mim = 15.00
    max = 18.00
    wenhao = '先生你好'
elif sex_input=='女':
    #这里是elif,不是else
    mim = 25.00
    max = 28.00
    wenhao = '女士你好'
result = mim<score<max
if result:
    tishi = '恭喜你,你的身体非常健康'
elif score<mim:
    tishi = '请注意,你的身体偏瘦'
else:
    tishi = '请注意,你的身体偏胖'
print(wenhao,tishi)

# #输出
# if (sex == 1 and 15<=score<= 18)or(sex == 0 and 25<=score<=28):
#         print('您的体脂率合格!')
# else:
#         print('您的体脂率不合格。')

你可能感兴趣的:(Python,python,开发语言)