Skip to content

Added a habit tracker that saves daily progress to a json file #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions habit_tracker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json
import datetime
habits={
1:"Drink water",
2:"Walk the dog",
3:"Run",
4:"Read a book"
}

try:
with open("data.json", "r") as f:
data = json.load(f)
except FileNotFoundError:
data = {}

x = datetime.datetime.now()
today_date= str(str(x.year)+ "-"+str(x.month) +"-"+ str(x.day))
if today_date not in data:
data[today_date] = {}

def tracker():
completed_habits=0
for habit_id in habits:
user_answer = input(f"{habits[habit_id]} ? (y/n)").lower()
habit_name= habits[habit_id]
if user_answer == "y":
value = True
completed_habits +=1
elif user_answer == "n":
value = False
else:
print("Incorrect input!")
return
data[today_date][habit_name] = value
status= "Amazing job !" if completed_habits == len(habits) else "Keep it going !"
print(f"You have completed {completed_habits}/{len(habits)} of your habits! {status}")
tracker()
with open('data.json', 'w') as f:
json.dump(data, f)