¿Como calcular cuantas calorias al día se deben consumir, usando Python?
Lunes, 30 de mayo de 2011
Está bien, es una excusa barata para usar Python para calcular una formula trivial
. Sin embargo la idea es útil:
- Usted quiere saber cuantas calorías debe consumir al día para mantener un peso saludable
- Sigue la proporción 40/ 30 / 30: %40 de proteínas, %30 de hidratos de carbono y %30 de grasas
- Usted come 6 veces al día, para mantener su metabolismo activo
Por ejemplo, la salida del programa para una persona que pesa 196 libras:
1 2 3 4 5 | Macintosh:python josevnz$ ./calories.py Please provide your weight in LB: 196 Daily calories for 196 lb: 2640. Grams of Protein: 44/264, Grams of Carbs: 33/198, Grams of Fat: 14/88 Macintosh:python josevnz$ |
programa que llama a la clase en Python:
1 2 3 4 5 6 7 8 9 10 11 12 | #!/usr/bin/env python # Author: josevnz at kodegeek dot com # http://kodegeek.com/blog from sys import stdin from com.kodegeek.fitness.nutrition import CalorieNeeds if __name__ == "__main__": print "Please provide your weight in LB:" weight = int(stdin.readline()) c = CalorieNeeds(weight) print "%s" % c |
Y la clase en Python que hace el trabajo ‘pesado’:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #!/usr/bin/env python class CalorieNeeds: """Simple class to determine the daily calorie intake. josevnz at kodegeek dot com""" def __init__(self, weight): self.weight = weight # Calculate daily calorie needs self.calorieIntake = (self.weight * 15) - 300 # Calculate the 40/30/30 macronutriend breakdown # Proteins self.proteinsGramsDay = (self.calorieIntake * 0.40) / 4 self.proteinsGramsPerMeal = self.proteinsGramsDay / 6 # Carbs self.carbGramsDay = (self.calorieIntake * 0.30) / 4 self.carbGramsPerMeal = self.carbGramsDay / 6 # Fat self.fatGramsDay = (self.calorieIntake * 0.30) / 9 self.fatGramsPerMeal = self.fatGramsDay / 6 def __str__(self): return "Daily calories for %s lb: %d. Grams of Protein: %d/%d, Grams of Carbs: %d/%d, Grams of Fat: %d/%d" % (self.weight, self.calorieIntake, self.proteinsGramsPerMeal, self.proteinsGramsDay, self.carbGramsPerMeal, self.carbGramsDay, self.fatGramsPerMeal, self.fatGramsDay) if __name__ == "__main__": """Self test code""" c = CalorieNeeds(158) print "%s" % c |
Les dejo como ejercicio como convertir estos resultados a kilogramos, hoy tengo algo de pereza (el enlace para bajarse el código está aquí)
–José
Comentarios recientes