Code / Python for Beginners / Unit 5: Functions, text and your project

Final project: UPI expense tracker फ़ाइनल प्रोजेक्ट: UPI ख़र्च ट्रैकर

Lesson 14 of 14 · about 25 minutes · Read expenses, total them per category and find the biggest.

⚡ 0 XP

Step 1 of 2

Warm-up: read one expense

weight gsweet 1–10label1808🥭 mango602🍋 lemon2109🥭 mango

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:

  1. parts = line.split(",") gives ["food", "120"]
  2. category = parts[0].strip().lower() so that Food and food count as the same
  3. amount = int(parts[1]) (remember the int() trap!)

🎯 Challenge: Split one expense +20 XP

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.

Try twice to unlock the solution (0/2)

main.pyCtrl ⏎
Output

What your program prints appears here.

Checklist

Press Run to check your code.

  • amount is the int 120
  • Prints category and amount
  • Cleans Travel, 45
Lesson notes (text version)

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:

  1. parts = line.split(",") gives ["food", "120"]
  2. category = parts[0].strip().lower() so that Food and food count as the same
  3. amount = 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:

  1. Ask how many expenses there are (int!)
  2. Loop that many times; read and split each line
  3. 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
  4. Loop over totals.items() and print each category
  5. Print the grand total with sum(totals.values())
  6. 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.

Search by task, format or tool name.