Ravindra BagaleCourses & study guides मराठी Track your progress

Python Mini Projects for Beginners: 5 Practice Projects Before Learning pandas

P13.5 Python Project 5: Multi-Currency Sales Report Saved to JSON

Why. Global companies sell in $, £, € and ₹. A report must convert everything to one currency and save it for other tools.

What. We convert each sale to USD with a rate dictionary, total by country with a class-free approach, and save the result as JSON.

How.

Make the report

  1. Write the sales and the exchange rates.
  2. Write a convert function that raises an error for unknown currencies.
  3. Total USD by country.
  4. Save the summary with json.dump and read it back.

Worked example A — Convert, total and save.

Step 1 — What we do. Sales of an IT services firm in four countries.

Step 2 — Type and run this code.

import json
rates_to_usd = {"USD": 1.0, "GBP": 1.27, "EUR": 1.08, "INR": 0.012}
sales = [
    ("USA", 12000, "USD"), ("UK", 8000, "GBP"),
    ("Germany", 9500, "EUR"), ("India", 650000, "INR"), ("UK", 1500, "GBP"),
]
def to_usd(amount, cur):
    if cur not in rates_to_usd:
        raise ValueError(f"Unknown currency {cur}")
    return amount * rates_to_usd[cur]
by_country = {}
for country, amt, cur in sales:
    by_country[country] = round(by_country.get(country, 0) + to_usd(amt, cur), 2)
report = {"currency": "USD", "total": round(sum(by_country.values()), 2), "by_country": by_country}
with open("report.json", "w", encoding="utf-8") as f:
    json.dump(report, f, indent=2)
print(open("report.json", encoding="utf-8").read())

Step 3 — Output you should see.

{
  "currency": "USD",
  "total": 42125.0,
  "by_country": {
    "USA": 12000.0,
    "UK": 12065.0,
    "Germany": 10260.0,
    "India": 7800.0
  }
}

Step 4 — What it means. Tuples are unpacked in the loop, a function protects against unknown currencies, and the result is saved for Power BI, Excel or a web app.

Ravindra Bagale's Tip

Keep exchange rates in one dictionary (or one file). When rates change, you edit one place.