Health Tracker

Simple Health Tracker in Python

pythonCopy codeimport json
from datetime import datetime

class HealthTracker:
    def __init__(self):
        self.data = {
            'exercises': [],
            'meals': [],
            'weights': []
        }
        self.load_data()

    def load_data(self):
        try:
            with open('health_data.json', 'r') as f:
                self.data = json.load(f)
        except FileNotFoundError:
            self.data = {'exercises': [], 'meals': [], 'weights': []}

    def save_data(self):
        with open('health_data.json', 'w') as f:
            json.dump(self.data, f, indent=4)

    def add_exercise(self, name, duration, calories_burned):
        exercise = {
            'date': str(datetime.now().date()),
            'name': name,
            'duration': duration,
            'calories_burned': calories_burned
        }
        self.data['exercises'].append(exercise)
        self.save_data()
        print(f"Added exercise: {exercise}")

    def add_meal(self, name, calories):
        meal = {
            'date': str(datetime.now().date()),
            'name': name,
            'calories': calories
        }
        self.data['meals'].append(meal)
        self.save_data()
        print(f"Added meal: {meal}")

    def add_weight(self, weight):
        weight_record = {
            'date': str(datetime.now().date()),
            'weight': weight
        }
        self.data['weights'].append(weight_record)
        self.save_data()
        print(f"Added weight: {weight_record}")

    def show_summary(self):
        print("\n--- Health Tracker Summary ---")
        print("\nExercises:")
        for exercise in self.data['exercises']:
            print(exercise)

        print("\nMeals:")
        for meal in self.data['meals']:
            print(meal)

        print("\nWeights:")
        for weight in self.data['weights']:
            print(weight)

if __name__ == "__main__":
    tracker = HealthTracker()
    
    while True:
        print("\n1. Add Exercise")
        print("2. Add Meal")
        print("3. Add Weight")
        print("4. Show Summary")
        print("5. Exit")

        choice = input("Choose an option: ")

        if choice == '1':
            name = input("Enter exercise name: ")
            duration = input("Enter duration (in minutes): ")
            calories_burned = input("Enter calories burned: ")
            tracker.add_exercise(name, duration, calories_burned)
        
        elif choice == '2':
            name = input("Enter meal name: ")
            calories = input("Enter calories: ")
            tracker.add_meal(name, calories)
        
        elif choice == '3':
            weight = input("Enter your weight: ")
            tracker.add_weight(weight)
        
        elif choice == '4':
            tracker.show_summary()
        
        elif choice == '5':
            break
        
        else:
            print("Invalid option. Please try again.")

How to Use This Tracker

  1. Run the Script: Save the script as health_tracker.py and run it with Python.
  2. Menu Options: You’ll see a menu to add exercises, meals, weights, or view the summary.
  3. Data Storage: The tracker saves data in a health_data.json file in the same directory, so it persists between runs.

Extensions and Improvements

  • Data Visualization: You can use libraries like matplotlib or seaborn to create graphs of your progress over time.
  • User Interface: Consider creating a GUI with libraries like tkinter for a more user-friendly experience.
  • Health Metrics: Add more health metrics (like sleep hours, blood pressure) to track.
  • Reminders: Implement reminders for activities or meal logging.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *