程序的异常处理

try:

  <语块句1>

except:

  <语块句2>

else:

  <语块句3>

finally:

  <语句块4>

finally 对应语块句4一定执行,else对应语块句3不发生异常时执行

实例:身体质量指数BMI(Body Mass Index)

heigh,weight = eval(input("请输入身高(米)和体重(公斤 )[逗号隔开]:"))

bmi = weight/pow(heigh,2);

print("BMI数值为:{.2f}".format(bmi));

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 = "偏胖","肥胖"

posted @ 2019-05-28 22:45  任小炎  阅读(187)  评论(0编辑  收藏  举报