Today This Free Friday I am working on a project on YouTube a Personal Finance Tracker part 2.
from datetime import datetime
date_format = "%d-%m-%Y"
CATEGORIES = {"I": "Income", "E": "EXpense"}
def get_date(prompt, allow_default=False):
date_str = input(prompt)
if allow_default and not date_str:
return datetime.today().strtime(date_format)
try:
valid_date = datetime.strftime(date_str, date_format)
return valid_date.strftime(date_format)
except ValueError:
print("Invalid date format. Please enter the date in dd-mm-yyyy format")
return get_date(prompt, allow_default)
def get_amount():
try:
amount = float(input("Enter the amount: "))
if amount <= 0:
raise ValueError("Amount must be a non-negative non-zero value.")
return amount
except ValueError as e:
print(e)
return get_amount()
def get_category():
category = input("Enter the category ('I' for Income or 'E' for Expense): ").upper()
if category in CATEGORIES:
return CATEGORIES[category]
print("Invalid category. Please enter 'I' for Income or 'E' for Expense.")
return get_category()
def get_descriptipn():
return input("Enter a description (optional): ")
- This part of the code is so that I have a place where I can write all of the functions related to getting information from the user.
date_format = "%d-%m-%Y"
def get_date(prompt, allow_default=False):
date_str = input(prompt)
if allow_default and not date_str:
return datetime.today().strtime(date_format)
try:
valid_date = datetime.strftime(date_str, date_format)
return valid_date.strftime(date_format)
except ValueError:
print("Invalid date format. Please enter the date in dd-mm-yyyy format")
return get_date(prompt, allow_default)
- This part of the code is to cleans up the Date that the user type in and gives it to the user in the format that user needs.
def add():
CSV.initializes_csv()
date = get_date("Enter the date of the transaction (dd-mm-yyyy) or enter for today's date: ", allow_default=True)
amount = get_amount()
category = get_category()
description = get_descriptipn()
CSV.add_entry(date, amount, category, description)
add()
- This part of the code is to write a function that will call these Functions in the order that the user wants