diff --git a/tdee_calculator.py b/tdee_calculator.py new file mode 100644 index 0000000..8e7efc1 --- /dev/null +++ b/tdee_calculator.py @@ -0,0 +1,259 @@ +# -*- coding: utf-8 -*- +"""BMI 分类与每日热量需求(BMR / TDEE)交互式计算器 + +用法 +---- +1) 交互式(推荐,一路回车即可使用默认值): + python tdee_calculator.py +2) 命令行一次性给出参数: + python tdee_calculator.py --sex female --age 30 --height 163 --weight 50 --steps 10000 --goal maintain + +算法 +---- +1. BMR(基础代谢率):Mifflin-St Jeor 与 Schofield 两个公式取平均 + Mifflin : 10*体重kg + 6.25*身高cm - 5*年龄 + (男 +5 / 女 -161) + Schofield: 男 <18 岁 17.7*W+657 | 成年 15.3*W+679 + 女 <18 岁 13.4*W+692 | 成年 14.7*W+496 +2. PAL(活动系数):由每天步数换算 + 连续式:PAL = 1.2 + 步数/1000 * 0.04,钳制在 1.2 ~ 1.95 + 分档式:<5000 →1.15~1.25 | 5000~ →1.30~1.45 | 7500~ →1.45~1.55 + 10000~→1.50~1.60 | ≥12500→1.60~1.80 +3. TDEE(每日总消耗)= BMR * PAL +4. 目标摄入:维持 = TDEE;增重 = TDEE + 300~500; + 减重 = TDEE * 0.825,且不低于 BMR 与安全下限(男 1500 / 女 1200 kcal) + +说明:本工具仅作健康科普参考,不构成医疗建议。 +""" + +from __future__ import annotations + +import argparse +import sys + +SAFE_FLOOR = {"male": 1500, "female": 1200} # 减重安全下限 kcal/天 + + +# -------------------------------------------------------------------------- +# 输入部分 +# -------------------------------------------------------------------------- +def parse_args(argv=None): + parser = argparse.ArgumentParser( + description="BMI / BMR / TDEE 计算器(不给参数则进入交互式输入)" + ) + parser.add_argument("--sex", choices=["male", "female"], help="性别:male / female") + parser.add_argument("--age", type=float, help="年龄(岁)") + parser.add_argument("--height", type=float, help="身高(cm)") + parser.add_argument("--weight", type=float, help="体重(kg)") + parser.add_argument("--steps", type=float, help="每日步数") + parser.add_argument( + "--goal", + choices=["maintain", "gain", "cut"], + default=None, + help="目标:maintain 维持 / gain 增重 / cut 减重", + ) + return parser.parse_args(argv) + + +def prompt_value(label, unit, cast, default=None, lo=None, hi=None): + """反复询问直到拿到合法数值;直接回车则用默认值。""" + while True: + tip = f"(回车默认 {default:g}{unit})" if default is not None else "" + raw = input(f"{label}({unit}){tip}:").strip() + if raw == "" and default is not None: + return cast(default) + try: + value = cast(raw) + except ValueError: + print(" × 请输入数字,例如 163 或 51.5") + continue + if (lo is not None and value < lo) or (hi is not None and value > hi): + print(f" × 数值需在 {lo:g} ~ {hi:g} 之间") + continue + return value + + +def prompt_sex(default="female"): + while True: + tip = "女" if default == "female" else "男" + raw = input(f"性别(男/女,回车默认{tip}):").strip().lower() + if raw == "": + return default + if raw in ("男", "m", "male", "1"): + return "male" + if raw in ("女", "f", "female", "0", "2"): + return "female" + print(" × 请输入 男 或 女") + + +def prompt_goal(default="maintain"): + mapping = {"1": "maintain", "2": "gain", "3": "cut"} + while True: + print("目标:1) 维持体重 2) 增重 3) 减重") + raw = input("请选择(回车默认 1):").strip() + if raw == "": + return default + if raw in mapping: + return mapping[raw] + print(" × 请输入 1 / 2 / 3") + + +def collect_interactive(vals): + """交互式收集输入,上一轮的值作为默认值。""" + vals["sex"] = prompt_sex(vals.get("sex") or "female") + vals["age"] = prompt_value("年龄", "岁", float, vals.get("age") or 30, 6, 120) + vals["height"] = prompt_value("身高", "cm", float, vals.get("height") or 165, 100, 220) + vals["weight"] = prompt_value("体重", "kg", float, vals.get("weight") or 55, 20, 200) + vals["steps"] = prompt_value( + "每日步数", "步", float, + vals.get("steps") if vals.get("steps") is not None else 10000, 0, 60000, + ) + vals["goal"] = prompt_goal(vals.get("goal") or "maintain") + return vals + + +# -------------------------------------------------------------------------- +# 计算部分 +# -------------------------------------------------------------------------- +def bmi_of(weight_kg, height_cm): + h = height_cm / 100.0 + return weight_kg / (h * h) + + +def bmi_class(bmi, age): + if age < 18: + return "未成年人(成人阈值不适用,请用年龄别 BMI 百分位判定)" + if bmi < 18.5: + return "偏瘦" + if bmi < 24: + return "正常" + if bmi < 28: + return "超重" + return "肥胖" + + +def bmr_mifflin(weight_kg, height_cm, age, sex): + base = 10 * weight_kg + 6.25 * height_cm - 5 * age + return base + (5 if sex == "male" else -161) + + +def bmr_schofield(weight_kg, age, sex): + if sex == "male": + return 17.7 * weight_kg + 657 if age < 18 else 15.3 * weight_kg + 679 + return 13.4 * weight_kg + 692 if age < 18 else 14.7 * weight_kg + 496 + + +def pal_from_steps(steps): + """连续式 PAL。""" + return max(1.2, min(1.95, 1.2 + steps / 1000.0 * 0.04)) + + +def pal_bucket(steps): + """分档式 PAL 区间,用作上下限估计。""" + if steps < 5000: + return 1.15, 1.25 + if steps < 7500: + return 1.30, 1.45 + if steps < 10000: + return 1.45, 1.55 + if steps < 12500: + return 1.50, 1.60 + return 1.60, 1.80 + + +def target_intake(tdee, bmr, goal, sex): + """返回 (下限, 上限, 目标说明)。""" + if goal == "maintain": + return tdee, tdee, "维持体重" + if goal == "gain": + return tdee + 300, tdee + 500, "健康增重(每日 +300~500 kcal)" + val = max(tdee * 0.825, bmr, SAFE_FLOOR[sex]) + return val, val, "减重(约 -17.5%,不低于 BMR 与安全下限)" + + +# -------------------------------------------------------------------------- +# 输出部分 +# -------------------------------------------------------------------------- +def report(sex, age, height, weight, steps, goal): + sex_cn = "男" if sex == "male" else "女" + bmi = bmi_of(weight, height) + mifflin = bmr_mifflin(weight, height, age, sex) + schofield = bmr_schofield(weight, age, sex) + bmr = (mifflin + schofield) / 2 + pal = pal_from_steps(steps) + pal_lo, pal_hi = pal_bucket(steps) + tdee = bmr * pal + t_lo, t_hi, goal_desc = target_intake(tdee, bmr, goal, sex) + + h = height / 100.0 + w20, w21 = 20 * h * h, 21 * h * h + + print() + print("=" * 58) + print(f"个人信息:{sex_cn},{age:.0f} 岁,{height:.0f} cm,{weight:.1f} kg,每天 {steps:.0f} 步") + print(f"BMI:{bmi:.1f} → {bmi_class(bmi, age)}") + print("-" * 58) + print(f"基础代谢 BMR:{bmr:.0f} kcal/天" + f"(Mifflin {mifflin:.0f} · Schofield {schofield:.0f},两者取平均)") + print(f"活动系数 PAL:{pal:.2f}(按步数分档约 {pal_lo:.2f} ~ {pal_hi:.2f})") + print(f"每日总消耗 TDEE:{tdee:.0f} kcal/天" + f"(合理区间约 {bmr * pal_lo:.0f} ~ {bmr * pal_hi:.0f})") + print("-" * 58) + print(f"目标:{goal_desc}") + if abs(t_hi - t_lo) < 1: + print(f"建议每日摄入:{t_lo:.0f} kcal/天") + else: + print(f"建议每日摄入:{t_lo:.0f} ~ {t_hi:.0f} kcal/天") + if goal == "cut": + print("(若已触及 BMR 或安全下限,说明热量已压到最低,请勿再减)") + + prot_lo, prot_hi = (1.5 * weight, 1.8 * weight) if age < 20 else (1.2 * weight, 1.6 * weight) + print(f"蛋白质建议:{prot_lo:.0f} ~ {prot_hi:.0f} g/天;" + f"脂肪约 {weight * 0.8:.0f} g/天以上;其余热量由主食与果蔬补足。") + + if age < 18: + print("⚠ 未满 18 岁:成人 BMI 阈值不适用,请用年龄别 BMI 百分位(WS/T 586-2018)判定," + "并请医生用 EER 青少年公式复核热量。") + if bmi < 20: + need_lo, need_hi = max(0.0, w20 - weight), max(0.0, w21 - weight) + tip = (f"还需增重 {need_lo:.1f} ~ {need_hi:.1f} kg" + if need_hi > 0.05 else "体重已在目标区间内") + print(f"⚠ 你的 BMI 贴近偏瘦边缘:目标可设在 BMI 20~21,即体重 " + f"{w20:.1f} ~ {w21:.1f} kg({tip})。建议按 TDEE +300~500 kcal 吃,不要减重。") + + print("-" * 58) + print("提示:步数只能粗略估计活动量(快走/慢走可差 ±15%),此为本阶段的起始值;") + print(" 执行 2~3 周后看体重趋势再微调 ±100~200 kcal。本工具不构成医疗建议。") + print("=" * 58) + + +# -------------------------------------------------------------------------- +# 入口 +# -------------------------------------------------------------------------- +def main(argv=None): + args = parse_args(argv) + given = { + "sex": args.sex, + "age": args.age, + "height": args.height, + "weight": args.weight, + "steps": args.steps, + "goal": args.goal, + } + interactive = any(given[k] is None for k in ("sex", "age", "height", "weight", "steps")) + vals = dict(given) + + while True: + if interactive: + vals = collect_interactive(vals) + report(vals["sex"], vals["age"], vals["height"], + vals["weight"], vals["steps"], vals["goal"] or "maintain") + if not interactive: + break + if input("\n再算一次?(y/N):").strip().lower() not in ("y", "yes", "是"): + print("再见!") + break + return 0 + + +if __name__ == "__main__": + sys.exit(main())