My Blog

My WordPress Blog

My Blog

My WordPress Blog

Step 1: Set Up Your Environment

You’ll need Python installed on your machine. You can download it from python.org.

Step 2: Create a New Python File

Create a new file called finance_tracker.py.

Step 3: Basic Structure

Here’s a basic outline of your finance tracker:

import json
import os

class FinanceTracker:
def __init__(self):
    self.file_path = 'finance_data.json'
    self.data = self.load_data()
def load_data(self):
    if os.path.exists(self.file_path):
        with open(self.file_path, 'r') as file:
            return json.load(file)
    return {'income': [], 'expenses': []}
def save_data(self):
    with open(self.file_path, 'w') as file:
        json.dump(self.data, file)
def add_income(self, amount, source):
    self.data['income'].append({'amount': amount, 'source': source})
    self.save_data()
def add_expense(self, amount, category):
    self.data['expenses'].append({'amount': amount, 'category': category})
    self.save_data()
def get_balance(self):
    total_income = sum(item['amount'] for item in self.data['income'])
    total_expenses = sum(item['amount'] for item in self.data['expenses'])
    return total_income - total_expenses
def display(self):
    print("Income:")
    for item in self.data['income']:
        print(f"Source: {item['source']}, Amount: {item['amount']}")
    print("\nExpenses:")
    for item in self.data['expenses']:
        print(f"Category: {item['category']}, Amount: {item['amount']}")
    print(f"\nCurrent Balance: {self.get_balance()}")
def main():
tracker = FinanceTracker()
while True:
    print("\nPersonal Finance Tracker")
    print("1. Add Income")
    print("2. Add Expense")
    print("3. View Data")
    print("4. Exit")
    choice = input("Choose an option: ")
    if choice == '1':
        amount = float(input("Enter income amount: "))
        source = input("Enter source of income: ")
        tracker.add_income(amount, source)
    elif choice == '2':
        amount = float(input("Enter expense amount: "))
        category = input("Enter category of expense: ")
        tracker.add_expense(amount, category)
    elif choice == '3':
        tracker.display()
    elif choice == '4':
        break
    else:
        print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()

Step 4: Explanation of the Code

  1. Imports: We import json for data storage and os for file handling.
  2. FinanceTracker Class: This class handles loading, saving, adding income/expenses, calculating balance, and displaying data.
    • load_data: Loads data from a JSON file.
    • save_data: Saves the current data to a JSON file.
    • add_income and add_expense: Methods to add income and expenses.
    • get_balance: Calculates the current balance.
    • display: Displays all income and expenses, along with the current balance.
  3. Main Function: This function provides a simple text menu for user interaction.

Step 5: Running the Application

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you saved finance_tracker.py.
  3. Run the script using the command:
python finance_tracker.py

Step 6: Features to Consider Adding

Once you have the basic tracker running, you can consider adding features like:

  • Categories for income and expenses.
  • Graphical representation of your financial data using libraries like matplotlib.
  • Monthly reports or summaries.
  • Data export options (e.g., to CSV).
Step 1: Set Up Your Environment

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top