├── README.md └── budget_tracker.py /README.md: -------------------------------------------------------------------------------- 1 | # Budget_tracker 2 | Simple budget traker written in python 3 | -------------------------------------------------------------------------------- /budget_tracker.py: -------------------------------------------------------------------------------- 1 | 2 | import json 3 | 4 | def add_expense(expenses, description, amount): 5 | expenses.append({"description": description, "amount": amount}) 6 | print(f"Added expense: {description}, Amount: {amount}") 7 | 8 | def get_total_expenses(expenses): 9 | return sum(expense['amount'] for expense in expenses) 10 | 11 | def get_balance(budget, expenses): 12 | return budget - get_total_expenses(expenses) 13 | 14 | def show_budget_details(budget, expenses): 15 | print(f"Total Budget: {budget}") 16 | print("Expenses:") 17 | for expense in expenses: 18 | print(f"- {expense['description']}: {expense['amount']}") 19 | print(f"Total Spent: {get_total_expenses(expenses)}") 20 | print(f"Remaining Budget: {get_balance(budget, expenses)}") 21 | 22 | def load_budget_data(filepath): 23 | try: 24 | with open(filepath, 'r') as file: 25 | data = json.load(file) 26 | return data['initial_budget'], data['expenses'] 27 | except (FileNotFoundError, json.JSONDecodeError): 28 | return 0, [] # Return default values if the file doesn't exist or is empty/corrupted 29 | # 30 | def save_budget_data(filepath, initial_budget, expenses): 31 | data = { 32 | 'initial_budget': initial_budget, 33 | 'expenses': expenses 34 | } 35 | with open(filepath, 'w') as file: 36 | json.dump(data, file, indent=4) 37 | 38 | 39 | def main(): 40 | print("Welcome to the Budget App") 41 | initial_budget = float(input("Please enter your initial budget: ")) 42 | # filepath = 'budget_data.json' # Define the path to your JSON file 43 | # initial_budget, expenses = load_budget_data(filepath) 44 | budget = initial_budget 45 | expenses = [] 46 | 47 | while True: 48 | print("\nWhat would you like to do?") 49 | print("1. Add an expense") 50 | print("2. Show budget details") 51 | print("3. Exit") 52 | choice = input("Enter your choice (1/2/3): ") 53 | 54 | if choice == "1": 55 | description = input("Enter expense description: ") 56 | amount = float(input("Enter expense amount: ")) 57 | add_expense(expenses, description, amount) 58 | elif choice == "2": 59 | show_budget_details(budget, expenses) 60 | elif choice == "3": 61 | print("Exiting Budget App. Goodbye!") 62 | break 63 | else: 64 | print("Invalid choice, please choose again.") 65 | 66 | if __name__ == "__main__": 67 | main() --------------------------------------------------------------------------------