Python Forum
Find Longest streak for habits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find Longest streak for habits
#1
Sad 
HI
I am creating a habit tracking application and have to find the longest Streak for a given habit. for ex:- if a user swim for 5 straight days than he create a streak of 5 days. But i want if the user miss to chekoff the habit on any day. That mean he breaks his streak .
than the streak in table 1 get back to 0. But in table 2 it should be at 5 days.

Here are my two tables

def create_tables(db):
    cur = db.cursor()

    cur.execute("""CREATE TABLE IF NOT EXISTS Habit (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    task_specification TEXT,
    period TEXT,
    created_at BOOLEAN,
    streak INT ) """)

    cur.execute("""CREATE TABLE IF NOT EXISTS count (
    name TEXT,
    created_at BOOLEAN,
    FOREIGN KEY (name) REFERENCES Habit(name))""")

    db.commit()
functions i have created in the database
# find longest streak for specified habit

def longest_streak(db, name):
    cur = db.cursor()
    cur.execute('SELECT streak FROM count WHERE name=?', (name,))
    long = cur.fetchall()
    for row in long:
        print("Here is your longest streak ")
        print(row[0])


# find longest streak for all habits in the database

def longest_of_all(db):
    cur = db.cursor()

    cur.execute('SELECT name, MAX(streak) FROM count')
    long_all = cur.fetchall()
    for row in long_all:
        print("Here is your longest streak of all habits")
        print(row[0] + row[1])
Actually my functions are not working as i want . Please help me to make it work
Gribouillis write Sep-10-2022, 05:41 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
What is your thinking here? What is this function supposed to do?
def longest_streak(db, name):
    cur = db.cursor()
    cur.execute('SELECT streak FROM count WHERE name=?', (name,))
    long = cur.fetchall()
    for row in long:
        print("Here is your longest streak ")
        print(row[0])
Reply
#3
(Sep-11-2022, 10:10 PM)deanhystad Wrote: What is your thinking here? What is this function supposed to do?
def longest_streak(db, name):
    cur = db.cursor()
    cur.execute('SELECT streak FROM count WHERE name=?', (name,))
    long = cur.fetchall()
    for row in long:
        print("Here is your longest streak ")
        print(row[0])

This function suppose to find the longest streak of given habit.
the user enter the name of the habit name of the habit than

command line interface

if long == "Longest Streak Of Specific Habit":
lon_streak = questionary.text("Enter the name of habit for which you want to see the longest streak").ask()
get_longest_streak(db, lon_streak)

analytic.py file

def get_longest_streak(db, name):
"""

:param db: an initiated habit data
:param streak:number of days of continuity
:return:number of days in integer
"""
list = longest_streak(db, name)
return list

Attached Files

Thumbnail(s)
   
Reply
#4
I don't see where your "count" database contains a "streak" field.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print the longest str from user input edwdas 5 4,174 Nov-04-2019, 02:02 PM
Last Post: perfringo
  Print The Length Of The Longest Run in a User Input List Ashman111 3 3,213 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020