From 1315af5ed3db32c233ed72e641398784a49cdb4c Mon Sep 17 00:00:00 2001 From: class03m09 Date: Thu, 24 Sep 2026 02:43:16 +0000 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=20calorie=5Fcli.py=20?= =?UTF-8?q?=E7=9A=84=E4=B8=89=E5=A4=A7=E8=90=A5=E5=85=BB=E7=B4=A0=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 把三大营养素的计算抽成 macros() 函数,蛋白质、脂肪、碳水的口径与网页版保持一致 - 脂肪按总热量的 27% 折算,碳水按补足剩余热量折算,去掉重复的中间换算 - 超重与肥胖界值改为参数传入,默认 24.6 / 26.4 --- calorie_cli.py | 56 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/calorie_cli.py b/calorie_cli.py index 282ec01..ae74390 100644 --- a/calorie_cli.py +++ b/calorie_cli.py @@ -22,6 +22,11 @@ PAL_CHOICES = { 2.0: "高强度(几乎每天训练)", } +# 脂肪供能占比 +FAT_RATIO = 0.27 +# 碳水与蛋白质每克热量 +KCAL_PER_GRAM = 4 + def bmr(sex: str, weight_kg: float, height_cm: float, age: int) -> float: """基础代谢率(kcal/天)。 @@ -70,6 +75,24 @@ def target_intake(tdee: float, goal: str, age: int, sex: str) -> tuple: return tdee, False +def macros(intake: float, weight_kg: float, age: int) -> dict: + """按建议摄入算出三大营养素克数。""" + teen = age < 18 + protein_low = (1.4 if teen else 1.2) * weight_kg + protein_high = 1.6 * weight_kg + protein_mid = (protein_low + protein_high) / 2 + fat_mid = FAT_RATIO * intake / 9 + carb_mid = max(0.0, (intake - protein_mid * KCAL_PER_GRAM - fat_mid * 9) / KCAL_PER_GRAM) + return { + "protein_low": protein_low, + "protein_high": protein_high, + "protein_mid": protein_mid, + "fat_low": 0.25 * intake / 9, + "fat_high": 0.30 * intake / 9, + "carb_mid": carb_mid, + } + + def micros(age: int, sex: str): """维生素与矿物质每日需要量。""" teen = age < 18 @@ -125,12 +148,7 @@ def main() -> None: intake, clamped = target_intake(tdee, args.goal, args.age, args.sex) teen = args.age < 18 - p_low = (1.4 if teen else 1.2) * weight_kg - p_high = 1.6 * weight_kg - p_mid = (p_low + p_high) / 2 - fat_low = 0.25 * intake / 9 - fat_high = 0.30 * intake / 9 - carb_mid = max(0.0, (intake - p_mid * 4 - fat_mid_kcal(intake) / 9 * 9) / 4) + m = macros(intake, weight_kg, args.age) if teen: water = "1500–2000 ml" @@ -146,19 +164,22 @@ def main() -> None: print(line) print("每日热量与营养素需求计算器") print(line) - print("输入:身高 {:.0f} cm,体重 {:.1f} kg,{} 岁,{}".format(args.height, weight_kg, args.age, sex_cn)) - print(" 活动系数 {}({}),目标:{}".format(args.pal, PAL_CHOICES[args.pal], goal_cn)) + print("输入:身高 {:.0f} cm,体重 {:.1f} kg,{} 岁,{}".format( + args.height, weight_kg, args.age, sex_cn)) + print(" 活动系数 {}({}),目标:{}".format( + args.pal, PAL_CHOICES[args.pal], goal_cn)) print() print("BMI ............. {:.1f} {}".format( bmi, judge_bmi(bmi, args.age, args.teen_over, args.teen_obese))) print("基础代谢 BMR .... {:.0f} kcal".format(base)) print("每日总消耗 TDEE . {:.0f} kcal".format(tdee)) - print("建议每日摄入 .... {:.0f} kcal{}".format(intake, "(已触发下限保护)" if clamped else "")) + print("建议每日摄入 .... {:.0f} kcal{}".format( + intake, "(已触发下限保护)" if clamped else "")) print() print("三大营养素(每日)") - print(" 蛋白质 ......... {:.0f}–{:.0f} g".format(p_low, p_high)) - print(" 脂肪 ........... {:.0f}–{:.0f} g".format(fat_low, fat_high)) - print(" 碳水化合物 ..... {:.0f} g".format(carb_mid)) + print(" 蛋白质 ......... {:.0f}–{:.0f} g".format(m["protein_low"], m["protein_high"])) + print(" 脂肪 ........... {:.0f}–{:.0f} g".format(m["fat_low"], m["fat_high"])) + print(" 碳水化合物 ..... {:.0f} g".format(m["carb_mid"])) print(" 膳食纤维 ....... 25–30 g") print(" 饮水 ........... {}".format(water)) print() @@ -169,16 +190,15 @@ def main() -> None: print("三餐分配") for label, ratio in (("早餐", 0.25), ("午餐", 0.35), ("加餐", 0.10), ("晚餐", 0.30)): print(" {} {:>5.0f} kcal 蛋白质 {:>3.0f} g 碳水 {:>3.0f} g".format( - label, intake * ratio, p_mid * ratio, carb_mid * ratio)) + label, + intake * ratio, + m["protein_mid"] * ratio, + m["carb_mid"] * ratio, + )) print() print("提示:公式误差约 ±10%;青少年 BMI 判定请以 WS/T 586-2018 原表为准;") print(" 未成年人减重请先咨询医生或营养师,每日摄入不低于 2000 kcal。") -def fat_mid_kcal(intake: float) -> float: - """脂肪按总热量的 27% 折算(kcal)。""" - return 0.27 * intake - - if __name__ == "__main__": main()