1. Warm-up: read one expense
Every UPI payment has a category and an amount. We will type each one as a line like food,120.
To use it, split the line at the comma and clean both parts:
parts = line.split(",") gives ["food", "120"]category = parts[0].strip().lower() so that Food and food count as the sameamount = int(parts[1]) (remember the int() trap!)
Challenge: Split one expense
Read one expense line like food,120. Split it at the comma. Store the cleaned, lowercase name in category and the number in amount (an int). Print Category: food and Amount: ₹120. For Travel, 45 it must print Category: travel and Amount: ₹45.
2. Build the tracker
Time to use everything you learned! The plan:
- Ask how many expenses there are (int!)
- Loop that many times; read and split each line
- Add each amount to a dict of totals:
totals[category] = totals.get(category, 0) + amount. The .get(…, 0) trick starts a new category at 0 - Loop over
totals.items() and print each category - Print the grand total with
sum(totals.values()) - Find the biggest category:
max(totals, key=totals.get) gives the key whose value is largest
You can also do step 6 with a loop and a "best so far" variable, like the cricket overs.
totals = {}
for cat in ["chai", "auto", "chai"]:
totals[cat] = totals.get(cat, 0) + 10
print(totals)
print(max(totals, key=totals.get))
Challenge: UPI expense tracker
Read the number of expenses, then that many lines like food,120. Clean each category with .strip().lower(). Keep a dict totals of category → total amount. Then print one line per category in the order first seen, like food: ₹350, then Total spent: ₹749, then Biggest: food (₹350).
With the sample Inputs (food 120, travel 60, food 230, recharge 299, travel 40) the summary is: food ₹350, travel ₹100, recharge ₹299, total ₹749, biggest food.