python实现BMI计算
直接上代码~~
#coding=utf-8
# 主要练习输入&输出+异常捕捉,熟悉基本程序流
import os
for a in range(1, 4):
try:
high = float(input("输入你的身高(m):"))
weight = float(input("输入你的体重(kg):"))
if (high <= 0 or high > 3) or (weight <= 0 or weight > 200): # 判断身高&体重合理值范围
print("请输入真实的身高或者体重,谢谢配合!")
continue
except BaseException as msg:
if a == 3:
print("你输入的身高或体重有误,但是你没有机会了", a)
else:
print("你输入的身高或体重有误,请核对后重新输入", a)
else:
print("你的身高是:%.2f(m),体重是:%.2f(kg)" % (high, weight))
BMI = float(weight/(high*high))
print("你的BMI指数是:", round(BMI, 2))
break
os.system("pause")
最后一行是为了封装为exe,不会闪退,python封装为exe的教程参考:https://www.cnblogs.com/yifengyu/p/15572924.html
老铁,如果觉得本文对你有帮助,麻烦帮点个右下角的“推荐”,感谢!
本文来自博客园,作者:平行时空的旅者,转载请注明原文链接:https://www.cnblogs.com/yifengyu/p/16254383.html
浙公网安备 33010602011771号