-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhabit_tracker.py
More file actions
215 lines (174 loc) · 5.97 KB
/
habit_tracker.py
File metadata and controls
215 lines (174 loc) · 5.97 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
Atomic Habits inspired tracker with a simple "character growth" system.
Each habit has:
- cue: what triggers the habit
- action: what you actually do
- reward: why it matters to you
- streak: how many days in a row you have done it
- total_completions: total count of completions
Each completion gives XP to your character.
Your character gains levels as XP increases.
"""
from typing import List, Dict, Any
Habit = Dict[str, Any]
Character = Dict[str, Any]
def default_habits() -> List[Habit]:
return [
{
"name": "Workout",
"cue": "See gym clothes and shoes ready",
"action": "45 minute lift session",
"reward": "Feel strong, confident, and clear",
"done": False,
"streak": 0,
"total_completions": 0,
},
{
"name": "Code",
"cue": "Sit at desk, open VS Code",
"action": "At least 2 hours of focused Python",
"reward": "Move closer to tech job and freedom",
"done": False,
"streak": 0,
"total_completions": 0,
},
{
"name": "Read",
"cue": "Put phone away, book on desk",
"action": "Read at least 10 pages",
"reward": "Stronger mind, deeper thinking",
"done": False,
"streak": 0,
"total_completions": 0,
},
]
def create_character(name: str = "Vaelis") -> Character:
return {
"name": name,
"level": 1,
"xp": 0,
}
def xp_for_next_level(level: int) -> int:
"""Simple formula for required XP per level."""
return 100 * level
def grant_xp(character: Character, amount: int) -> None:
character["xp"] += amount
while character["xp"] >= xp_for_next_level(character["level"]):
character["xp"] -= xp_for_next_level(character["level"])
character["level"] += 1
print(f"\n{character['name']} leveled up to Level {character['level']}!")
def view_character(character: Character) -> None:
print("\nCharacter status:")
print(f"Name: {character['name']}")
print(f"Level: {character['level']}")
print(f"XP: {character['xp']} / {xp_for_next_level(character['level'])}")
def view_habits(habits: List[Habit]) -> None:
if not habits:
print("No habits yet.")
return
print("\nYour habits:")
for index, habit in enumerate(habits, start=1):
status = "✅" if habit["done"] else "❌"
print(f"{index}. {habit['name']} - {status}")
print(f" Cue: {habit['cue']}")
print(f" Action: {habit['action']}")
print(f" Reward: {habit['reward']}")
print(f" Streak: {habit['streak']} | Total: {habit['total_completions']}")
print()
def add_habit(habits: List[Habit]) -> None:
name = input("Habit name: ").strip()
if not name:
print("Habit name cannot be empty.")
return
cue = input("Cue (what triggers it): ").strip()
action = input("Action (what you do): ").strip()
reward = input("Reward (why it matters): ").strip()
habits.append(
{
"name": name,
"cue": cue,
"action": action,
"reward": reward,
"done": False,
"streak": 0,
"total_completions": 0,
}
)
print(f"Added habit: {name}")
def remove_habit(habits: List[Habit]) -> None:
if not habits:
print("No habits to remove.")
return
view_habits(habits)
choice_str = input("Enter the number of the habit to remove: ").strip()
try:
choice = int(choice_str)
if not 1 <= choice <= len(habits):
print("Invalid choice.")
return
except ValueError:
print("Please enter a valid integer.")
return
removed = habits.pop(choice - 1)
print(f"Removed habit: {removed['name']}")
def complete_habit(habits: List[Habit], character: Character) -> None:
if not habits:
print("No habits to complete. Add one first.")
return
view_habits(habits)
choice_str = input("Enter the number of the habit you completed: ").strip()
try:
choice = int(choice_str)
if not 1 <= choice <= len(habits):
print("Invalid choice.")
return
except ValueError:
print("Please enter a valid integer.")
return
habit = habits[choice - 1]
if habit["done"]:
print(f"You already marked '{habit['name']}' as done today.")
return
habit["done"] = True
habit["streak"] += 1
habit["total_completions"] += 1
print(f"\nNice. You completed: {habit['name']}")
print(f"Reward: {habit['reward']}")
print(f"Streak: {habit['streak']} | Total completions: {habit['total_completions']}")
grant_xp(character, amount=10)
def reset_day(habits: List[Habit]) -> None:
for habit in habits:
habit["done"] = False
print("Reset all habits for a new day. Streaks stay, done flags cleared.")
def main() -> None:
habits = default_habits()
character = create_character("Vaelis")
while True:
print("\nAtomic Habit Tracker")
print("1. View habits")
print("2. Add habit")
print("3. Remove habit")
print("4. Complete habit")
print("5. View character")
print("6. Reset day")
print("7. Exit")
choice = input("Choose an option (1-7): ").strip()
if choice == "1":
view_habits(habits)
elif choice == "2":
add_habit(habits)
elif choice == "3":
remove_habit(habits)
elif choice == "4":
complete_habit(habits, character)
elif choice == "5":
view_character(character)
elif choice == "6":
reset_day(habits)
elif choice == "7":
print("Exiting. Keep building your identity through action.")
break
else:
print("Invalid choice. Please enter a number from 1 to 7.")
if __name__ == "__main__":
main()