|
| 1 | +``` |
| 2 | +""" |
| 3 | +Expense Tracker - Simple Console App |
| 4 | +-------------------------------------- |
| 5 | +A small Python project to record daily expenses. |
| 6 | +Data is saved to a CSV file so that it persists between runs. |
| 7 | +
|
| 8 | +Concepts used: |
| 9 | +- File handling (read/write) |
| 10 | +- Loops and conditionals |
| 11 | +- Dictionaries |
| 12 | +- Basic input validation |
| 13 | +""" |
| 14 | +
|
| 15 | +import csv |
| 16 | +import os |
| 17 | +
|
| 18 | +# The file where expenses will be saved |
| 19 | +FILENAME = "expenses.csv" |
| 20 | +
|
| 21 | +
|
| 22 | +def initialize_file(): |
| 23 | + """Creates the CSV file if it doesn't exist.""" |
| 24 | + if not os.path.exists(FILENAME): |
| 25 | + with open(FILENAME, mode="w", newline="") as file: |
| 26 | + writer = csv.writer(file) |
| 27 | + writer.writerow(["Category", "Description", "Amount"]) |
| 28 | + print(f"Created new file: {FILENAME}\n") |
| 29 | +
|
| 30 | +
|
| 31 | +def add_expense(): |
| 32 | + """Adds a new expense entry.""" |
| 33 | + category = input("Enter category (e.g., Food, Travel, Bills): ").strip() |
| 34 | + description = input("Enter short description: ").strip() |
| 35 | + try: |
| 36 | + amount = float(input("Enter amount spent (in ₹): ")) |
| 37 | + except ValueError: |
| 38 | + print("Invalid amount. Please enter a number.\n") |
| 39 | + return |
| 40 | +
|
| 41 | + with open(FILENAME, mode="a", newline="") as file: |
| 42 | + writer = csv.writer(file) |
| 43 | + writer.writerow([category, description, amount]) |
| 44 | +
|
| 45 | + print("Expense added successfully!\n") |
| 46 | +
|
| 47 | +
|
| 48 | +def view_expenses(): |
| 49 | + """Displays all recorded expenses.""" |
| 50 | + if not os.path.exists(FILENAME): |
| 51 | + print("No expenses recorded yet.\n") |
| 52 | + return |
| 53 | +
|
| 54 | + print("\n--- Expense Records ---") |
| 55 | + with open(FILENAME, mode="r") as file: |
| 56 | + reader = csv.reader(file) |
| 57 | + next(reader) # Skip header |
| 58 | + total = 0 |
| 59 | + count = 0 |
| 60 | + for row in reader: |
| 61 | + if len(row) == 3: |
| 62 | + print(f"{row[0]:<10} | {row[1]:<20} | ₹{row[2]}") |
| 63 | + total += float(row[2]) |
| 64 | + count += 1 |
| 65 | + print(f"\nTotal entries: {count}") |
| 66 | + print(f"Total spent: ₹{total:.2f}\n") |
| 67 | +
|
| 68 | +
|
| 69 | +def clear_expenses(): |
| 70 | + """Deletes all data from the CSV file.""" |
| 71 | + confirm = input("Are you sure you want to delete all expenses? (y/n): ").lower() |
| 72 | + if confirm == "y": |
| 73 | + with open(FILENAME, mode="w", newline="") as file: |
| 74 | + writer = csv.writer(file) |
| 75 | + writer.writerow(["Category", "Description", "Amount"]) |
| 76 | + print("All expense records cleared.\n") |
| 77 | + else: |
| 78 | + print("Operation cancelled.\n") |
| 79 | +
|
| 80 | +
|
| 81 | +def main(): |
| 82 | + """Main program loop.""" |
| 83 | + initialize_file() |
| 84 | +
|
| 85 | + while True: |
| 86 | + print(""" |
| 87 | +===== EXPENSE TRACKER ===== |
| 88 | +1. Add Expense |
| 89 | +2. View Expenses |
| 90 | +3. Clear All Expenses |
| 91 | +4. Exit |
| 92 | +============================ |
| 93 | +""") |
| 94 | + choice = input("Choose an option (1-4): ").strip() |
| 95 | +
|
| 96 | + if choice == "1": |
| 97 | + add_expense() |
| 98 | + elif choice == "2": |
| 99 | + view_expenses() |
| 100 | + elif choice == "3": |
| 101 | + clear_expenses() |
| 102 | + elif choice == "4": |
| 103 | + print("Goodbye! Stay mindful of your spending.") |
| 104 | + break |
| 105 | + else: |
| 106 | + print("Invalid choice. Please select a valid option.\n") |
| 107 | +
|
| 108 | +
|
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
| 111 | +``` |
0 commit comments