廖雪峰教程的练习:
小明身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数:
- 低于18.5:过轻
- 18.5-25:正常
- 25-28:过重
- 28-32:肥胖
- 高于32:严重肥胖
用if-elif
判断并打印结果。
print('Hi there, plz input your name:')
name = input()
print('hi there, your name is:', name)
print('plz input your height(CM)')
height = input()
print('hi there, your height is:', height)
print('plz input your weight(KG)')
weight = input()
print('hi there, your weight is:', weight)
h = float(height)
w = float(weight)
BMI = w/h/h*10000
if BMI >= 32:
a = '严重肥胖'
elif BMI >=28:
a = '肥胖'
elif BMI >= 25:
a = '过重'
elif BMI >= 18.5:
a = '正常'
else:
a = '太轻'
print('hi %s, here is all your info:' %name)
print('your height and weight is %d cm,'%h)
print('your weight is %d kg' %w)
print('BMI = %.2f, ' %BMI, a)
身高体重本来是int,测试ok,但是体重输了个带小数点的就报错了,就换为float了,取小数点后两位。
其实最后三句,想合在一句写的,但是目前还没法搞定,试了好几种都错了。所以只能分开,或者最后四句写在一起。2020.5.5