How to make a simple family budget calculator

Question:

I am making a simple budget calculator and It is a little bit complicated for me as a beginner. I need to add elements to a list through a function and then print an overview of a specific month and a year. It should sum up the money in each category and print it out.

budget=[]
def add_element (day,month,year,money,category):
    budget.append(day,month,year,money,category)
    

def overview (month,year):
       
    
    


add_element(15,10,2022,150,"food")
add_element(16,11,2022,250,"food")
add_element(17,11,2022,300,"living")
add_element(18,11,2022,500,"food")
add_element(19,11,2022,150,"household")

print(overview(11,2022)) 

I am expecting this outcome:

{"food": 750, "household": 150, "living": 300}

Asked By: Jimmy

||

Answers:

I guess that overview should create a summary dictionary of budget data for a specific month and year by iterating via the budget, adding the money value for each matching month and year to the summary dictionary using the category as the key, and returning the summary dictionary

budget = []

def add_element(day, month, year, money, category):
    budget.append((day, month, year, money, category))

def overview(month, year):
    summary = {}
    for day, m, y, money, category in budget:
        if m == month and y == year:
            if category in summary:
                summary[category] += money
            else:
                summary[category] = money
    return summary

add_element(15, 10, 2022, 150, "food")
add_element(16, 11, 2022, 250, "food")
add_element(17, 11, 2022, 300, "living")
add_element(18, 11, 2022, 500, "food")
add_element(19, 11, 2022, 150, "household")

print(overview(11, 2022))  # {"food": 750, "household": 150, "living": 300}
Answered By: di.bezrukov

The below code should help you with your need.

def add_element(day, month, year, money, category):
        budget.append([day, month, year, money, category])
    
def overview(month, year):
        food_total = 0
        household_total = 0
        living_total = 0
        for item in budget:
            if item[1] == month and item[2] == year:
                if item[4] == "food":
                    food_total += item[3]
                elif item[4] == "household":
                    household_total += item[3]
                elif item[4] == "living":
                    living_total += item[3]
        return {"food": food_total, "household": household_total, "living": living_total}
    
budget = []
add_element(15,10,2022,150,"food")
add_element(16,11,2022,250,"food")
add_element(17,11,2022,300,"living")
add_element(18,11,2022,500,"food")
add_element(19,11,2022,150,"household")
print(overview(11,2022)) 
Answered By: apan

Similar with using defaultdict

from collections import defaultdict
budget = []

def add_element(*row):
    budget.append(row)

def overview(month, year):
    summary = defaultdict(int)
    for d, m, y, money, category in budget:
        if m == month and y == year:
            summary[category] += money
    return summary

add_element(15, 10, 2022, 150, "food")
add_element(16, 11, 2022, 250, "food")
add_element(17, 11, 2022, 300, "living")
add_element(18, 11, 2022, 500, "food")
add_element(19, 11, 2022, 150, "household")

print(overview(11, 2022))  # defaultdict(<class 'int'>, {"food": 750, "household": 150, "living": 300})

you can anytime convert to dict using dict(summary)

Answered By: Filip Hanes
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.