计算我每天需要摄入的热量:BMI 正常但接近偏瘦,按每天约 1 万步估算维持热量(Mifflin-St Jeor 基础代谢 × 活动系数 1.65),并以增重为目标在维持热量基础上每天增加约 300 千卡,替换原先按减脂计算的版本
This commit is contained in:
+105
-136
@@ -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 (
|
||||
<div className="max-w-2xl mx-auto p-6 bg-slate-50 rounded-2xl">
|
||||
<h2 className="text-2xl font-bold text-slate-800 mb-1">每日热量需求计算器</h2>
|
||||
<p className="text-sm text-slate-500 mb-6">Mifflin-St Jeor 公式 + 步数活动系数</p>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4 mb-6">
|
||||
<div className="col-span-2">
|
||||
<label className="block text-sm font-medium text-slate-600 mb-2">性别</label>
|
||||
<div className="flex gap-3">
|
||||
<button
|
||||
onClick={() => setSex('female')}
|
||||
className={`flex-1 py-2 rounded-lg border font-medium transition ${sex === 'female' ? 'bg-rose-500 text-white border-rose-500' : 'bg-white text-slate-600 border-slate-200'}`}
|
||||
>女</button>
|
||||
<button
|
||||
onClick={() => setSex('male')}
|
||||
className={`flex-1 py-2 rounded-lg border font-medium transition ${sex === 'male' ? 'bg-sky-500 text-white border-sky-500' : 'bg-white text-slate-600 border-slate-200'}`}
|
||||
>男</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
def bmi(weight_kg, height_cm):
|
||||
h = height_cm / 100.0
|
||||
return weight_kg / (h * h)
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-600 mb-2">年龄(岁)</label>
|
||||
<input type="number" value={age} onChange={e => setAge(Number(e.target.value))} className={inputCls} />
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-600 mb-2">身高(cm)</label>
|
||||
<input type="number" value={height} onChange={e => setHeight(Number(e.target.value))} className={inputCls} />
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-600 mb-2">体重(kg)</label>
|
||||
<input type="number" value={weight} onChange={e => setWeight(Number(e.target.value))} className={inputCls} />
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-600 mb-2">每日步数</label>
|
||||
<input type="number" value={steps} onChange={e => setSteps(Number(e.target.value))} className={inputCls} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="bg-white rounded-xl p-4 border border-slate-100">
|
||||
<div className="flex items-center gap-2 text-slate-500 text-sm mb-1"><Scale size={16} /> BMI</div>
|
||||
<div className="text-2xl font-bold text-slate-800">{bmi.toFixed(1)}</div>
|
||||
<div className={`text-sm font-medium ${cat.color}`}>{cat.text}</div>
|
||||
</div>
|
||||
|
||||
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 "肥胖"
|
||||
<div className="bg-white rounded-xl p-4 border border-slate-100">
|
||||
<div className="flex items-center gap-2 text-slate-500 text-sm mb-1"><Flame size={16} /> 基础代谢 BMR</div>
|
||||
<div className="text-2xl font-bold text-slate-800">{round(bmr)}</div>
|
||||
<div className="text-sm text-slate-400">千卡 / 天</div>
|
||||
</div>
|
||||
|
||||
<div className="bg-white rounded-xl p-4 border border-slate-100">
|
||||
<div className="flex items-center gap-2 text-slate-500 text-sm mb-1"><Footprints size={16} /> 活动系数</div>
|
||||
<div className="text-2xl font-bold text-slate-800">× {band.factor}</div>
|
||||
<div className="text-sm text-slate-400">{band.label}</div>
|
||||
</div>
|
||||
|
||||
def target_intake(tdee, bmr, goal, sex):
|
||||
"""根据目标返回每日摄入热量,并做安全下限保护"""
|
||||
floor = 1500 if sex == "male" else 1200
|
||||
<div className="bg-gradient-to-br from-emerald-500 to-teal-600 rounded-xl p-4 text-white">
|
||||
<div className="flex items-center gap-2 text-emerald-50 text-sm mb-1"><Activity size={16} /> 每日总消耗 TDEE</div>
|
||||
<div className="text-3xl font-bold">{round(tdee)}</div>
|
||||
<div className="text-sm text-emerald-50">千卡 / 天(维持体重)</div>
|
||||
</div>
|
||||
|
||||
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")
|
||||
<div className="col-span-2 bg-white rounded-xl p-4 border border-slate-100">
|
||||
<div className="text-sm font-medium text-slate-600 mb-2">建议目标热量</div>
|
||||
<div className="flex justify-between text-sm py-1">
|
||||
<span className="text-slate-500">增重(每周 +0.3 kg 左右)</span>
|
||||
<span className="font-semibold text-slate-800">{round(tdee + 300)} 千卡</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-sm py-1">
|
||||
<span className="text-slate-500">维持体重</span>
|
||||
<span className="font-semibold text-slate-800">{round(tdee)} 千卡</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
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()
|
||||
<p className="text-xs text-slate-400 mt-4">* 结果仅供参考,个体肌肉量、代谢与食物热效应等会影响实际需求。</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user