Skip to main content
2 of 4
Added logic for length of input given

Python CLI app for tracking budget

I'm making a CLI app for myself for tracking my budget. How can I improve this code? One glitch I'm still trying to find out is when I hit return accidentally, it gives an error. I do not know how to get around it yet. I have added the logic.

#!/usr/bin/python3

def t_lines():
    import os
    terminal_size = os.get_terminal_size()
    print("-" * terminal_size.columns)


def main():
    import sqlite3 as sql

    conn = sql.connect("expenses.db")
    cur = conn.cursor()

    cur.execute("CREATE TABLE IF NOT EXISTS expenses \
            (Date TEXT DEFAULT \"x\", \
            item TEXT DEFAULT \"x\", \
            category TEXT DEFAULT \"x\", \
            Price REAL DEFAULT 0, \
            Type TEXT DEFAULT \"e\")" )

    t_lines()
    
    print("Enter in the following format")

    print("Enter 0 to quit")

    print("Date (dd/mmm/yy), Item, Category, Price, Type ([e]xpense/[i]ncome)")
    
    print("Example:")
    print("12/nov/18, carrot, vegetable, 15.64, e")
    print("13/aug/18, salary, work, 10000, income")

    t_lines()

    user_input = input()
    # the vals below seems unnecessary
    # vals = user_input.split(',')

    while user_input != "0":
        vals = user_input.split(',')

        if len(vals) != 5:
            print("Enter correctly")
        else:
            cur.execute("INSERT INTO expenses \
                VALUES (?, ?, ?, ?, ?)", vals)
            conn.commit()
            user_input = input()
        

    conn.close()

if __name__ == '__main__':
    main()