CodingNeed.

4 · Build useful programs · 20 MIN

Project · Expense summary

Combine loops, objects, conditions, and functions.

Combine a loop over input["expenses"], a category comparison, and an accumulator. Sketch the data contract before coding. Check empty input and refunds. Use a deliberate rounding policy before adapting this exercise to real payments.

A budget sheet totals just the rows you are interested in.

Read the example

expenses = [{"category":"food", "amount":5}, {"category":"travel", "amount":3}]
print(expenses[0]["amount"])
Check the expected output
5

Your challenge

Input is {expenses: [{category, amount}], category}. Return the total amount for expenses whose category matches input["category"]. Do not mutate input.

Common trap

Only add an amount when its category matches the requested category.

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.