Self learned code using AI to learn AI
def enter_date(): date = input("Enter the date (YYYY-MM-DD): ") if date.strip() == "": print("No input provided.") else: print(f"Date entered: {date}") return date
def enter_expense(): amount = 0 expense = input("Enter the type of expense: ") if expense.strip() == "": print("No input provided.") else: print(f"Expense type entered: {expense}") amount = float(input("Enter the amount spent: ")) if amount <= 0: print("Amount must be greater than zero.") else: print(f"Amount entered: {amount}") return expense, amount
def write_expenses(date, expense, amount): with open("expenses2.txt", "a") as file: file.write(f"{date}, {expense}: {amount}\n") print("Expense recorded.")
def read_expenses(): try: with open("expenses2.txt", "r") as file: print("Recorded Expenses: ") total = 0 for line in file: line = line.strip() if not line: continue parts = line.split(", ") if len(parts) != 2: print(f"Skipping malformed line: {line}") continue date, rest = parts if ": " not in rest: print(f"Skipping malformed line: {line}") continue expense, amount = rest.split(": ") total += float(amount) print(f"Date: {date}, Expense: {expense}, Amount: {amount}") print(f"Total Amount Spent: {total}") except FileNotFoundError: print("No expenses recorded yet.")
def main(): Expense_log = {}
while True:
print("\nSpending Tracker Menu: ")
print("1. Enter a new expense")
print("2. View all expenses")
print("3. Exit")
choice = input("Choose an option (1-3): ")
if choice == "1":
date = enter_date()
expense, amount = enter_expense()
if date and expense and amount > 0:
write_expenses(date, expense, amount)
elif choice == "2":
read_expenses()
elif choice == "3":
print("Exiting the Spending Tracker. Goodbye!")
break
else:
print("Invalid choice. Please select a valid option.")
if name == "main": main()