diff --git a/tdee_calculator.py b/tdee_calculator.py index 167bd25..7001e14 100644 --- a/tdee_calculator.py +++ b/tdee_calculator.py @@ -1,154 +1,123 @@ -# -*- coding: utf-8 -*- -""" -每日热量需求估算器:BMR (Mifflin-St Jeor) -> TDEE -> 减脂目标 +import { useState } from 'react'; +import { Flame, Scale, Footprints, Activity } from 'lucide-react'; -输入:身高、体重、年龄、性别、每日步数 -输出:BMR、TDEE、减脂期每日摄入热量、三大营养素建议 +const stepBands = [ + { min: 0, max: 5000, factor: 1.2, label: '久坐(<5,000 步)' }, + { min: 5000, max: 7500, factor: 1.375, label: '轻度活动(5,000–7,499 步)' }, + { min: 7500, max: 10000, factor: 1.55, label: '中度活动(7,500–9,999 步)' }, + { min: 10000, max: 12500, factor: 1.65, label: '高度活动(10,000–12,499 步)' }, + { min: 12500, max: Infinity, factor: 1.8, label: '非常活跃(≥12,500 步)' }, +]; -用法: - 1. 修改下面 CONFIG 里的个人信息,然后直接运行;或 - 2. import 后调用 daily_calories(...) - -注意:未成年人(<18 岁)的 BMI 判定应使用年龄性别百分位标准 - (中国 WS/T 586-2018),而非成人 24/28 的切点。 -""" - -# ============ 在这里填入真实数据 ============ -CONFIG = { - "height_cm": 165, # 身高(厘米) - "weight_kg": 70, # 体重(公斤)—— 140 斤 - "age": 16, # 年龄 - "sex": "female", # "male" 或 "female" - "steps": 10000, # 每日平均步数 - "goal": "cut", # "maintain" 保持 / "cut" 减脂 / "gain" 增重 +function bandFor(steps) { + return stepBands.find(b => steps >= b.min && steps < b.max) || stepBands[stepBands.length - 1]; } -# ============================================== +function bmiCategory(bmi) { + if (bmi < 18.5) return { text: '偏瘦', color: 'text-amber-600' }; + if (bmi < 24) return { text: '正常', color: 'text-emerald-600' }; + if (bmi < 28) return { text: '超重', color: 'text-orange-600' }; + return { text: '肥胖', color: 'text-red-600' }; +} -def bmr_mifflin(weight_kg, height_cm, age, sex): - """基础代谢率 BMR,单位 kcal/天(Mifflin-St Jeor 公式)""" - if weight_kg <= 0 or height_cm <= 0: - raise ValueError("身高和体重必须大于 0") - if not 0 < age < 120: - raise ValueError("年龄看起来不太对") +export default function CalorieCalculator() { + const [sex, setSex] = useState('female'); + const [age, setAge] = useState(30); + const [height, setHeight] = useState(164); + const [weight, setWeight] = useState(51); + const [steps, setSteps] = useState(10000); - base = 10 * weight_kg + 6.25 * height_cm - 5 * age - if sex == "male": - return base + 5 - elif sex == "female": - return base - 161 - else: - raise ValueError("sex 只能是 'male' 或 'female'") + const hM = height / 100; + const bmi = hM > 0 ? weight / (hM * hM) : 0; + const cat = bmiCategory(bmi); + const bmr = sex === 'male' + ? 10 * weight + 6.25 * height - 5 * age + 5 + : 10 * weight + 6.25 * height - 5 * age - 161; -def pal_from_steps(steps): - """根据每日步数返回 (下限, 常用值, 上限) 三个 PAL 系数""" - if steps < 5000: - return 1.15, 1.2, 1.25 - elif steps < 7500: - return 1.3, 1.375, 1.45 - elif steps < 10000: - return 1.45, 1.5, 1.55 - elif steps < 12500: - return 1.5, 1.55, 1.6 - else: - return 1.6, 1.725, 1.8 + const band = bandFor(steps); + const tdee = bmr * band.factor; + const round = n => Math.round(n); + const inputCls = 'w-full px-3 py-2 rounded-lg border border-slate-200 focus:outline-none focus:ring-2 focus:ring-sky-400'; -def daily_calories(weight_kg, height_cm, age, sex, steps=10000): - """返回 (BMR, PAL, TDEE下限, TDEE常用, TDEE上限)""" - bmr = bmr_mifflin(weight_kg, height_cm, age, sex) - low, mid, high = pal_from_steps(steps) - return bmr, mid, bmr * low, bmr * mid, bmr * high + return ( +
+

每日热量需求计算器

+

Mifflin-St Jeor 公式 + 步数活动系数

+
+
+ +
+ + +
+
-def bmi(weight_kg, height_cm): - h = height_cm / 100.0 - return weight_kg / (h * h) +
+ + setAge(Number(e.target.value))} className={inputCls} /> +
+
+ + setHeight(Number(e.target.value))} className={inputCls} /> +
+
+ + setWeight(Number(e.target.value))} className={inputCls} /> +
+
+ + setSteps(Number(e.target.value))} className={inputCls} /> +
+
+
+
+
BMI
+
{bmi.toFixed(1)}
+
{cat.text}
+
-def bmi_class(b, age=None): - """BMI 分类;未成年人提示使用百分位标准""" - if age is not None and age < 18: - label = "青少年请按 WS/T 586-2018 年龄别百分位判定" - # 仅作粗略参考 - if b < 18.5: - rough = "偏瘦" - elif b < 23.5: - rough = "正常" - elif b < 26.5: - rough = "超重" - else: - rough = "肥胖" - return f"{rough}({label})" - if b < 18.5: - return "偏瘦" - elif b < 24.0: - return "正常" - elif b < 28.0: - return "超重" - else: - return "肥胖" +
+
基础代谢 BMR
+
{round(bmr)}
+
千卡 / 天
+
+
+
活动系数
+
× {band.factor}
+
{band.label}
+
-def target_intake(tdee, bmr, goal, sex): - """根据目标返回每日摄入热量,并做安全下限保护""" - floor = 1500 if sex == "male" else 1200 +
+
每日总消耗 TDEE
+
{round(tdee)}
+
千卡 / 天(维持体重)
+
- if goal == "maintain": - return tdee, "维持体重" - elif goal == "cut": - # 缺口 15%~20%,取中点 0.825 - val = tdee * 0.825 - val = max(val, bmr, floor) # 不低于 BMR 和安全线 - return val, "减脂(缺口约 17.5%)" - elif goal == "gain": - return tdee * 1.1, "增重(+10%)" - else: - raise ValueError("goal 只能是 maintain / cut / gain") +
+
建议目标热量
+
+ 增重(每周 +0.3 kg 左右) + {round(tdee + 300)} 千卡 +
+
+ 维持体重 + {round(tdee)} 千卡 +
+
+
- -def main(): - c = CONFIG - bmr, pal, low, mid, high = daily_calories( - c["weight_kg"], c["height_cm"], c["age"], c["sex"], c["steps"] - ) - - my_bmi = bmi(c["weight_kg"], c["height_cm"]) - target, goal_desc = target_intake(mid, bmr, c["goal"], c["sex"]) - deficit = mid - target - - # 1 kg 脂肪约 7700 kcal - weekly_loss = deficit * 7 / 7700 - - print("=" * 52) - print(f"个人信息: {c['sex']}, {c['age']}岁, " - f"{c['height_cm']}cm, {c['weight_kg']}kg") - print(f"BMI: {my_bmi:.1f} ({bmi_class(my_bmi, c['age'])})") - print(f"每日步数: {c['steps']}") - print("=" * 52) - print(f"基础代谢 BMR : {bmr:.0f} kcal") - print(f"活动系数 PAL : {pal}") - print(f"维持体重 TDEE: {mid:.0f} kcal/天 " - f"(合理区间 {low:.0f} ~ {high:.0f})") - print("-" * 52) - print(f"目标: {goal_desc}") - print(f"每日摄入: {target:.0f} kcal/天") - if c["goal"] == "cut": - print(f"热量缺口: 约 {deficit:.0f} kcal/天") - print(f"预计减重: 约 {weekly_loss:.2f} kg/周 " - f"({weekly_loss * 4:.1f} kg/月)") - print("-" * 52) - print(f"蛋白质建议: {c['weight_kg'] * 1.6:.0f} ~ " - f"{c['weight_kg'] * 2.0:.0f} g/天") - print(f"脂肪建议 : {c['weight_kg'] * 0.6:.0f} ~ " - f"{c['weight_kg'] * 0.8:.0f} g/天") - protein_kcal = c["weight_kg"] * 1.8 * 4 - fat_kcal = c["weight_kg"] * 0.7 * 9 - carb_g = max((target - protein_kcal - fat_kcal) / 4, 0) - print(f"碳水建议 : {carb_g:.0f} g/天") - print("=" * 52) - - -if __name__ == "__main__": - main() +

* 结果仅供参考,个体肌肉量、代谢与食物热效应等会影响实际需求。

+
+ ); +}