Sub topic 10
Reviewing the numbers and adjusting the habit
This chapter assumes you have built the earlier parts of the tracker. You should be comfortable with Python functions, dictionaries, sets, loops, and the date and timedelta types from datetime. You should also have check_in, current_streak, longest_streak and completion_rate working. Here you use those functions to decide what to change about a habit.
Why numbers need a review step
A tracker that only prints numbers does not help much. The benefit comes when you read the numbers on a schedule and act on them. A weekly review is a good default, because a week is long enough to show a pattern and short enough to fix a problem before it sets in.
Each metric answers a different question:
| Metric | Question it answers | Weakness |
|---|---|---|
| Current streak | Am I on a run right now? | One missed day resets it to 0. |
| Longest streak | What have I managed at my best? | It never goes down, so it can hide recent decline. |
| Completion rate | How often do I do it over a fixed window? | It ignores the order of days, so it cannot tell a run from scattered days. |
No single number is enough. A habit with a current streak of 2, a longest streak of 40 and a 30-day rate of 0.5 tells a specific story: you were once consistent, you slipped recently, and you are only now recovering.
Gathering the numbers in one place
Start with a function that collects all three metrics for one habit and returns them as a dictionary. This keeps the review logic separate from printing, so you can test it.
from datetime import date, timedelta
def current_streak(days, today=None):
today = today or date.today()
day = today if today in days else today - timedelta(days=1)
streak = 0
while day in days:
streak += 1
day -= timedelta(days=1)
return streak
def longest_streak(days):
best = run = 0
previous = None
for day in sorted(days):
if previous is not None and day - previous == timedelta(days=1):
run += 1
else:
run = 1
best = max(best, run)
previous = day
return best
def completion_rate(days, end, window=30):
start = end - timedelta(days=window - 1)
done = sum(1 for d in days if start <= d <= end)
return done / window
def summarize(days, today=None):
today = today or date.today()
return {
"current": current_streak(days, today),
"longest": longest_streak(days),
"rate_7": completion_rate(days, today, window=7),
"rate_30": completion_rate(days, today, window=30),
}
if __name__ == "__main__":
today = date(2026, 11, 27)
days = {today - timedelta(days=n) for n in (1, 2, 4, 5, 6, 9, 15)}
print(summarize(days, today))The example data has check-ins on six of the last seven days except days 3 and 7 ago, so the output shows a current streak of 2 (counted back from yesterday, because today is not logged), a longest streak of 3, and two completion rates. Passing today explicitly makes the result repeatable. If you relied on date.today(), the output would change every day.
Two windows are included on purpose. The 7-day rate reacts quickly to a change. The 30-day rate is steadier and shows the long-term level. Comparing them is the core of the review.
Comparing a short window with a long one
When the short-window rate is lower than the long-window rate, the habit is weakening. When it is higher, the habit is improving. A small gap is noise. A gap of about 0.2 or more is worth noticing, which is roughly 1.4 days out of 7.
def trend(rate_short, rate_long, threshold=0.2):
diff = rate_short - rate_long
if diff >= threshold:
return "improving"
if diff <= -threshold:
return "declining"
return "steady"
print(trend(0.43, 0.70)) # declining
print(trend(0.86, 0.60)) # improving
print(trend(0.71, 0.65)) # steadyThe threshold of 0.2 is a judgment call, not a rule. With a 7-day window, one day changes the rate by about 0.14, so a threshold below that would flag every single missed day. Choose a threshold larger than one day's effect on the shorter window.
Turning numbers into a recommendation
The flow above can be written as a small function. It returns a short message, not an action, because the decision is yours. The code only points at the most likely problem.
def review(summary):
rate_7 = summary["rate_7"]
rate_30 = summary["rate_30"]
direction = trend(rate_7, rate_30)
if rate_30 >= 0.8:
return "Strong. Keep it, and consider making it a little harder."
if direction == "improving":
return "Recovering. Change nothing and keep going."
if direction == "declining":
return "Slipping. Make the habit smaller or change when you do it."
return "Stuck at a low rate. Check that the habit is defined clearly."
summary = {"current": 2, "longest": 3, "rate_7": 0.43, "rate_30": 0.23}
print(review(summary)) # Slipping. Make the habit smaller or change when you do it.The function depends on trend from the previous section, so keep both in the same module. Notice the order of the checks. The strong case comes first, so a habit at 0.9 over 30 days is never called "slipping" just because the last week had two misses.
What adjusting actually means
The review points to a category of change. Four adjustments cover most cases.
- Shrink it. If the rate is declining, the habit is probably too big for your current days. Reading for 30 minutes becomes reading one page. A smaller habit is easier to log, and a longer streak is worth more than a short, ambitious one.
- Move the cue. A cue is the event that reminds you to start, such as finishing breakfast. If misses cluster on certain days, the cue may not exist on those days. Attach the habit to something that happens every day.
- Redefine it. A low and flat rate often means the yes/no test is unclear. "Exercise" is vague. "Walk for 10 minutes" can be answered honestly. Go back to the chapter on what makes a habit trackable and rewrite the check-in rule.
- Raise it. If the rate stays at or above 0.8, the habit is established. Add a small increase, then give the new version a few weeks of data before judging it.
When you change a habit, treat the new version as a new habit in the log. Track it under a new name, such as read-1-page, instead of reusing read. Otherwise the old and new check-ins mix, and the streaks and rates no longer describe one consistent rule.
Finding where the misses fall
A rate tells you how often you miss, not when. Counting misses by weekday often shows the real cause.
from collections import Counter
from datetime import date, timedelta
def misses_by_weekday(days, end, window=28):
names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
counts = Counter()
for n in range(window):
day = end - timedelta(days=n)
if day not in days:
counts[names[day.weekday()]] += 1
return counts
end = date(2026, 11, 27) # a Friday
days = {end - timedelta(days=n) for n in range(28) if (end - timedelta(days=n)).weekday() < 5}
print(misses_by_weekday(days, end))In this example the habit was done every weekday and skipped every weekend, so Sat and Sun each show 4 misses and no other day appears. A window of 28 days covers each weekday exactly four times, which makes the counts easy to compare. The fix here is not more willpower. It is a different cue for weekends, or a schedule that does not require weekends at all.
Common mistakes in reviewing
- Chasing the streak. Treating the current streak as the goal leads to logging a token check-in to protect the number. Check the completion rate too. If a high streak comes with a low-effort habit, shrink or redefine it.
- Judging too early. Fewer than 14 days of data makes every rate unreliable. In a new window, the 30-day rate divides by 30 even if you started 5 days ago, so it understates your real consistency. Wait until the window is mostly filled before drawing conclusions.
- Changing too many things at once. If you shrink the habit, move the cue, and rename it in the same week, you cannot tell which change helped.
- Reading a reset as failure. One miss sets the current streak to 0, but the longest streak and the rate barely change. Look at the rate first after a miss.
Handling a young habit
You can fix the young-habit problem by shrinking the window to the habit's age. This uses only the earliest check-in date, which is the closest the data gets to a start date.
from datetime import date, timedelta
def completion_rate(days, end, window=30):
start = end - timedelta(days=window - 1)
done = sum(1 for d in days if start <= d <= end)
return done / window
def adaptive_rate(days, end, window=30):
if not days:
return 0.0
age = (end - min(days)).days + 1
return completion_rate(days, end, window=min(window, age))
end = date(2026, 11, 27)
days = {end - timedelta(days=n) for n in range(10)}
print(completion_rate(days, end)) # 0.333...
print(adaptive_rate(days, end)) # 1.0There is a trade-off. The first check-in is not necessarily the day you started. If you meant to begin on day 1 and missed the first three days, the earliest date is day 4, and the rate looks better than it should. For a strict review, store an explicit start date for each habit. For a casual one, the adaptive rate is a reasonable approximation.
Exercises
Exercise
A habit has a 7-day rate of 0.57 and a 30-day rate of 0.60. The current streak is 1. Using trend with the default threshold and then review, what message do you get, and why is the current streak a poor basis for concern here?
Show answer
The difference is 0.57 - 0.60 = -0.03, which is above -0.2, so trend returns "steady". The 30-day rate is below 0.8, so review returns "Stuck at a low rate. Check that the habit is defined clearly." A streak of 1 only means yesterday or today was logged and the day before was missed. It says little about a habit done about 60% of the time, so the rate is the better guide.
Exercise
Using misses_by_weekday, a 28-day window shows Counter({'Mon': 4, 'Tue': 1}). What does this suggest, and which adjustment from this chapter fits best?
Show answer
The misses concentrate on Mondays, and each of the four Mondays in the window was missed. The Tuesday miss is probably noise. The best fit is to move the cue, because something about Mondays, such as a different morning routine, removes the existing reminder. Attach the habit to an event that also happens on Monday.
Exercise
Write a function needs_more_data(days, end, minimum=14) that returns True when the habit has fewer than minimum days of history, so a review can skip it. Use the earliest check-in as the start.
Show answer
def needsmoredata(days, end, minimum=14): if not days: return True age = (end - min(days)).days + 1 return age < minimum