Κουίζ Γενικών Γνώσεων Επόμενη

Habit Tracker


import json from datetime import datetime, timedelta class HabitTracker: def __init__(self, user_name): self.user_name = user_name self.habits = {} self.load_data() def load_data(self): try: with open(f"{self.user_name}_habits.json", "r") as file: self.habits = json.load(file) except FileNotFoundError: pass def save_data(self): with open(f"{self.user_name}_habits.json", "w") as file: json.dump(self.habits, file, indent=4) def add_habit(self, habit_name, frequency): self.habits[habit_name] = { "frequency": frequency, "start_date": str(datetime.today().date()), "streak": 0, "last_checked": None } self.save_data() print(f"Added habit: {habit_name} to track {frequency} days a week.") def check_habit(self, habit_name): if habit_name in self.habits: today = str(datetime.today().date()) habit = self.habits[habit_name] last_checked = habit.get("last_checked") if last_checked != today: habit["streak"] += 1 habit["last_checked"] = today self.save_data() print(f"Checked {habit_name}, streak increased to {habit['streak']}.") else: print(f"You have already checked this habit today.") else: print(f"Habit {habit_name} not found.") def show_habits(self): print(f"Habits for {self.user_name}:") for habit_name, details in self.habits.items(): last_checked = details['last_checked'] print(f"- {habit_name}: Streak {details['streak']}, Last checked: {last_checked if last_checked else 'Never'}") if __name__ == "__main__": user = input("Enter your name: ") tracker = HabitTracker(user) while True: print("\n1. Add habit\n2. Check habit\n3. Show habits\n4. Exit") choice = input("Choose an option: ") if choice == '1': habit_name = input("Enter habit name: ") frequency = input("How many times per week (1-7)? ") tracker.add_habit(habit_name, int(frequency)) elif choice == '2': habit_name = input("Enter habit name to check: ") tracker.check_habit(habit_name) elif choice == '3': tracker.show_habits() elif choice == '4': break else: print("Invalid option.")

Comments

Popular posts from this blog