|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 ShireyFen 于 2026-8-28 20:19 编辑
- from colorama import Fore, Style, init
- import time
- init(autoreset=True)
- #自定义函数-引导用户输入正确的数据(带错误处理)
- def inputdate(date):
- while True: #循环至转换成功为止
- #捕捉异常
- try:
- num = float(input(date))
- if num <= 0:
- print(Fore.RED+"不能为零或小于零,请重新输入")
- continue
- print(Fore.GREEN+"成功输入,正在等待")
- return num
- except ValueError:
- print(Fore.RED+"输入错误,请重新输入(请输入数字)")
- #自定义函数-分类用户的MBI类型(定义BMI评估函数)
- def judge_the_type(outcome):
- bmi_low = 18.5
- bmi_high = 23.9
- #初始化数据
- judge_outcome = {
- "judge_type" :"", #评估结果
- "risk":0.0, #危险度
- "diff":0.00, #相差
- "is_normal":False
- }
- #区间定义:(上限,下限,分类,危险度)
- criterion = [
- (0, 18.5, "体重偏瘦,建议加强营养", 0.2),
- (18.5, 23.9, "体重正常", 0), # 注意上限包含
- (23.9, 25, "体重偏胖(超重),注意饮食和运动", 0.5),
- (25, 30, "肥胖初期,请尽快控制", 1),
- (30, 35, "一级肥胖,建议咨询医生", 2),
- (35, 40, "二级肥胖", 3),
- (40, float('inf'), "三级肥胖", 6)
- ]
- for low,high,judge_type,risk in criterion: #for循环依次处理
- if low<=outcome<=high:
- judge_outcome["judge_type"] = judge_type #导入分类
- judge_outcome["risk"] = risk #导入危险度
- if 18.5<=outcome<=23.9:
- judge_outcome["is_normal"] = True #定义健康区间由于后续
- if outcome<18.5:
- judge_outcome["diff"] = bmi_low-outcome #导入相差
- elif outcome>23.9:
- judge_outcome["diff"] = outcome-bmi_high #导入相差
- break #找到区间退出
- return judge_outcome
- #引用函数收集数据
- User_hight=inputdate("请输入身高(单位:m):")
- User_weight=inputdate("请输入你的体重(单位:kg):")
- #计算
- User_BMI=User_weight/User_hight**2
- #定义结果
- judge_outcome= judge_the_type(User_BMI)
- print(Fore.YELLOW+"数据收集完成,正在计算。。。")
- time.sleep(2)
- print(Fore.BLUE+"\n正在输出结果")
- print(Fore.BLACK+"***********************************************")
- time.sleep(4)
- #输出
- print(Fore.GREEN+f"你的BMI值为{User_BMI}") #输出bmi值
- print(judge_outcome["judge_type"]) #输出评估结果
- if judge_outcome["is_normal"]!=True : #健康区间不输出以下
- time.sleep(1)
- print(Fore.RED+f"危险度{judge_outcome["risk"]}")
- if judge_outcome["diff"] != 0:#危险度为零不输出以下
- if User_BMI<18.5:
- time.sleep(1)
- print(Fore.YELLOW+f"你与MBI正常范围下限相差{judge_outcome["diff"]}") #输出相差
- else:
- time.sleep(1)
- print(Fore.YELLOW+f'你与MBI正常范围上限相差{judge_outcome["diff"]}') #输出相差
- time.sleep(4)
- print(Fore.GREEN+"已显示所有结果\n程序已结束\n数据依照MBI国际标准") #结束
复制代码 我也是终于完成完善我的程序了 |
评分
-
查看全部评分
|