Question: I am doing an assignment to make a python code that does budget planning for each month. I will insert the code below. def load_expenses(month):
I am doing an assignment to make a python code that does budget planning for each month. I will insert the code below.
def load_expenses(month): """ Loads expenses from the corresponding month's expense file.
Parameters: month (str): The abbreviated month name (e.g., 'jan').
Returns: list of tuples: Each tuple contains (amount, category). """ FinalProj_csfr7 = f"{month}_expenses.txt" expenses = [] try: with open(FinalProj_csfr7, 'r') as file: for line in file: parts = line.strip().split(',') if len(parts) != 3: continue item, amount_str, category = parts try: amount = float(amount_str) expenses.append((amount, category.strip())) except ValueError: continue except FileNotFoundError: print(f"Warning: The file for {month} does not exist and will be skipped.") return expenses
def get_selected_months(): """ Prompts the user to enter the months to include in the budget.
Returns: list of str: Validated list of month abbreviations. """ valid_months = ['jan', 'feb', 'mar', 'apr', 'may'] user_input = input("Which months' expenses should be used to plan the budget: ") input_months = [month.strip().lower() for month in user_input.split(',')] selected_months = [] for month in input_months: if month in valid_months: selected_months.append(month) else: print(f"'{month}' is not a valid month and will be skipped.") return selected_months
def calculate_budget(selected_months, expenses_data): """ Calculates the monthly budget based on selected months' expenses.
Parameters: selected_months (list of str): List of selected month abbreviations.
When I run the code the code works but it does not read my files. It says they do not exist. What do I have to do for the code to be able to read my file. The file name that contains all the month expenses is called FinalProj_csfr7.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
