Files
class03m09 1315af5ed3 整理 calorie_cli.py 的三大营养素计算
- 把三大营养素的计算抽成 macros() 函数,蛋白质、脂肪、碳水的口径与网页版保持一致
- 脂肪按总热量的 27% 折算,碳水按补足剩余热量折算,去掉重复的中间换算
- 超重与肥胖界值改为参数传入,默认 24.6 / 26.4
2026-09-24 02:43:16 +00:00

205 lines
8.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""每日热量与营养素需求计算器(命令行版)。
与网页版使用同一套算法:BMI、BMR、TDEE、建议摄入、三大营养素、七大营养素、三餐分配。
用法示例:
python calorie_cli.py
python calorie_cli.py --height 178 --weight 177 --unit jin --age 15 --sex male --pal 1.6 --goal cut
python calorie_cli.py --age 30 --sex female --weight 60 --unit kg --pal 1.4
"""
from __future__ import annotations
import argparse
# 活动系数 PAL
PAL_CHOICES = {
1.4: "久坐(几乎不运动,上下学坐车)",
1.6: "轻度(每周运动 1–3 次 / 有体育课)",
1.8: "中度(每周运动 3–5 次)",
2.0: "高强度(几乎每天训练)",
}
# 脂肪供能占比
FAT_RATIO = 0.27
# 碳水与蛋白质每克热量
KCAL_PER_GRAM = 4
def bmr(sex: str, weight_kg: float, height_cm: float, age: int) -> float:
"""基础代谢率(kcal/天)。
18 岁以下用 Schofield 青少年公式,18 岁及以上用 Mifflin-St Jeor 公式。
"""
if age < 18:
if sex == "male":
return 17.686 * weight_kg + 658.2
return 13.384 * weight_kg + 692.6
if sex == "male":
return 10 * weight_kg + 6.25 * height_cm - 5 * age + 5
return 10 * weight_kg + 6.25 * height_cm - 5 * age - 161
def judge_bmi(bmi: float, age: int, teen_over: float, teen_obese: float) -> str:
"""BMI 分级。
成人按中国成人标准;未满 18 岁按传入的界值判定(默认 15 岁男性参考值)。
"""
if age >= 18:
if bmi < 18.5:
return "偏瘦"
if bmi < 24:
return "正常"
if bmi < 28:
return "超重"
return "肥胖"
if bmi >= teen_obese:
return "肥胖"
if bmi >= teen_over:
return "超重"
return "未达超重"
def target_intake(tdee: float, goal: str, age: int, sex: str) -> tuple:
"""返回(建议每日摄入 kcal, 是否触发了下限保护)。"""
if goal == "cut":
floor = 2000 if age < 18 else (1500 if sex == "male" else 1200)
value = tdee - 450
if value < floor:
return float(floor), True
return value, False
if goal == "bulk":
return tdee + 300, False
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
male = sex == "male"
return [
("钙", "mg", 1000 if teen else 800, "牛奶 300–500 ml、豆制品、小鱼小虾"),
("铁", "mg", (16 if male else 18) if teen else (12 if male else 20), "红肉、动物肝脏、动物血"),
("锌", "mg", (11.5 if male else 8.5) if teen else (12.5 if male else 7.5), "牛肉、牡蛎、坚果、蛋黄"),
("碘", "μg", 120, "加碘盐、海带、紫菜"),
("维生素A", "μg RAE", (820 if male else 630) if teen else (800 if male else 700), "胡萝卜、南瓜、肝脏、鸡蛋"),
("维生素D", "μg", 10, "晒太阳约 20 分钟;食物中含量很少"),
("维生素E", "mg", 14, "坚果、植物油"),
("维生素B1", "mg", (1.6 if male else 1.3) if teen else (1.4 if male else 1.2), "全谷物、猪肉、豆类"),
("维生素B2", "mg", (1.5 if male else 1.2) if teen else (1.4 if male else 1.2), "牛奶、鸡蛋、肝脏"),
("烟酸", "mg NE", 15 if male else 12, "肉类、鱼、花生"),
("维生素B6", "mg", 1.4, "肉、鱼、香蕉"),
("维生素B12", "μg", 2.4, "肉、蛋、奶"),
("维生素C", "mg", 100, "青椒、猕猴桃、橙子、西兰花"),
("镁", "mg", (320 if male else 300) if teen else (330 if male else 280), "坚果、全谷、深绿蔬菜"),
("钾", "mg", 2000, "香蕉、土豆、菠菜、豆类"),
("钠", "mg", "<2000", "越少越好(相当于盐 < 5 g)"),
]
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="每日热量与营养素需求计算器(命令行版)",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("--height", type=float, default=178, help="身高(cm)")
parser.add_argument("--weight", type=float, default=177, help="体重")
parser.add_argument("--unit", choices=["jin", "kg"], default="jin", help="体重单位")
parser.add_argument("--age", type=int, default=15, help="年龄(岁)")
parser.add_argument("--sex", choices=["male", "female"], default="male", help="性别")
parser.add_argument("--pal", type=float, choices=list(PAL_CHOICES), default=1.6, help="活动系数")
parser.add_argument("--goal", choices=["cut", "maintain", "bulk"], default="cut",
help="目标:cut 减脂 / maintain 维持 / bulk 增肌")
parser.add_argument("--teen-over", type=float, default=24.6,
help="青少年超重界值(未满 18 岁适用)")
parser.add_argument("--teen-obese", type=float, default=26.4,
help="青少年肥胖界值(未满 18 岁适用)")
return parser
def main() -> None:
args = build_parser().parse_args()
weight_kg = args.weight / 2 if args.unit == "jin" else args.weight
height_m = args.height / 100
bmi = weight_kg / (height_m ** 2)
base = bmr(args.sex, weight_kg, args.height, args.age)
tdee = base * args.pal
intake, clamped = target_intake(tdee, args.goal, args.age, args.sex)
teen = args.age < 18
m = macros(intake, weight_kg, args.age)
if teen:
water = "1500–2000 ml"
elif args.sex == "male":
water = "1700–2000 ml"
else:
water = "1500–1800 ml"
sex_cn = "男" if args.sex == "male" else "女"
goal_cn = {"cut": "减脂", "maintain": "维持体重", "bulk": "增肌 / 增重"}[args.goal]
line = "=" * 52
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()
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()
print("三大营养素(每日)")
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()
print("维生素与矿物质(每日需要量)")
for name, unit, value, source in micros(args.age, args.sex):
print(" {:<10}{}{:<9}{}".format(name, value, " " + unit, source))
print()
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,
m["protein_mid"] * ratio,
m["carb_mid"] * ratio,
))
print()
print("提示:公式误差约 ±10%;青少年 BMI 判定请以 WS/T 586-2018 原表为准;")
print(" 未成年人减重请先咨询医生或营养师,每日摄入不低于 2000 kcal。")
if __name__ == "__main__":
main()