Skip to main content
added 8 characters in body
Source Link
toolic
  • 15.8k
  • 6
  • 29
  • 217

I'm making a CLI appapplication for myself for tracking my budget. How can I improve this code?

#!/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()

I'm making a CLI app for myself for tracking my budget. How can I improve this code?

#!/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()

I'm making a CLI application for myself for tracking my budget. How can I improve this code?

#!/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()
deleted 178 characters in body; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

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()

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()

I'm making a CLI app for myself for tracking my budget. How can I improve this code?

#!/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()
Added logic for length of input given
Source Link

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. 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 do not know how to get around it yethave 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()

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.

#!/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()

    vals = user_input.split(',')

    while user_input != "0":
        vals = user_input.split(',')
        cur.execute("INSERT INTO expenses \
                VALUES (?, ?, ?, ?, ?)", vals)
        conn.commit()
        user_input = input()
        

    conn.close()

if __name__ == '__main__':
    main()

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()
Source Link
Loading