Assignment8
Assignment8
Objective:
Solve python problems using files.
Question:
1. Create a Python program using Files that allows users to log and track their personal or business
expenses. The program should read and write expense data to a file, enabling users to record new
transactions, view total expenses, and analyse spending over time.
Algorithm:
Code:
import os
if expenses:
print("\nLogged Expenses:")
for expense in expenses:
description, amount, date = expense.strip().split(',')
print(f"Description: {description}, Amount: {amount}, Date: {date}")
else:
print("No expenses recorded.")
total = 0.0
with open(expense_file, 'r') as file:
for line in file:
_, amount, _ = line.strip().split(',')
total += float(amount)
if not os.path.exists(expense_file):
print("No expenses recorded yet.")
return
found = False
print("\nMatching Expenses:")
for expense in expenses:
description, amount, date = expense.strip().split(',')
if search_term in description or search_term in date:
print(f"Description: {description}, Amount: {amount}, Date: {date}")
found = True
if not found:
print("No matching expenses found.")
if not os.path.exists(expense_file):
print("No expenses recorded yet.")
return
if found:
print("Expense deleted successfully.")
else:
print("Expense not found.")
if not os.path.exists(expense_file):
print("No expenses recorded yet.")
return
if found:
print("Expense modified successfully.")
else:
print("Expense not found.")
if choice == '1':
log_expense()
elif choice == '2':
view_expenses()
elif choice == '3':
calculate_total_expenses()
elif choice == '4':
search_expenses()
elif choice == '5':
delete_expense()
elif choice == '6':
modify_expense()
elif choice == '7':
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")
main()
Output:
PS C:\Users\KR SRINIVAS> & C:/Python313/python.exe c:/Desktop/Folders/College/SEM-
2/SD_Python/Assignment-8/a8-1.py
Logged Expenses:
Description: Transport, Amount: 100.0, Date: 01.04.25
Description: Merch, Amount: 200.0, Date: 17.04.25
Matching Expenses:
No matching expenses found.
Matching Expenses:
Description: Merch, Amount: 200.0, Date: 17.04.25
Logged Expenses:
Description: Logistics, Amount: 360.0, Date: 01.04.25
Learning Outcomes: