【MOOC语言程序设计】分支结构-实例5
分支结构-实例5:身体质量指数BMI
基本知识点:
- 分支结构
单分支结构,if;二分支结构(有紧凑结构)if,else;多分支结构(注意包含关系&变量取值范围的覆盖)if,elif,else
- 异常处理
- 实例5:身体质量指数BMI
try:
num = eval(input())
print(num**2)
except NameError: #标注异常类型后,仅响应异常;
#异常类型名字等同于变量
print()
#CalBMIv3.py
height,weight = eval(input())
bmi = weight/(height**2)
print("BMI数值为:{:.2f}".format(bmi))
who,nat = "","" #预定义变量who和nat类型为字符串,没有这行也可
if bmi < 18.5:
who,nat = "偏瘦","偏瘦"
elif 18.5 <= bmi < 24:
who,nat = "正常","正常"
elif 24<=bmi <25:
who,nat = "正常","偏胖"
elif 25 <= bmi <28:
who, nat = "偏胖","偏胖"
elif 28 <= bmi <30:
who,nat = "偏胖","肥胖"
else:
who,nat = "肥胖","肥胖"
print("BMI指标为:国际{},国内{}".format(who,nat))