Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Simple Currency Converter

Convert currencies through fixed example USD-relative rates. Students divide into a common base and multiply out without treating sample rates as live prices.

IntermediatePhase 6 - Category 5: Real-World Logic ProblemsExample 7 of 15
simple-currency-converter.py
1# Exchange rates (example)
2rates = {
3 'USD': 1.0,
4 'EUR': 0.85,
5 'GBP': 0.73,
6 'INR': 82.5
7}
8
9# Take inputs
10amount = float(input("Enter amount: "))
11from_currency = input("From currency (USD/EUR/GBP/INR): ").upper()
12to_currency = input("To currency (USD/EUR/GBP/INR): ").upper()
13
14# Convert
15if from_currency in rates and to_currency in rates:
16 # Convert to USD first, then to target
17 usd_amount = amount / rates[from_currency]
18 converted = usd_amount * rates[to_currency]
19 print(f"{amount} {from_currency} = {converted:.2f} {to_currency}")
20else:
21 print("Invalid currency")

Output

Enter amount: 100
From currency (USD/EUR/GBP/INR): USD
To currency (USD/EUR/GBP/INR): INR
100.0 USD = 8250.00 INR

In this one

  • Interpret base-relative rates
  • Validate both currency keys
  • Distinguish samples from live quotes

How it works

  1. 1

    Uppercase both currency codes.

  2. 2

    Divide by the source rate to obtain the USD base.

  3. 3

    Multiply by the destination rate and print the conversion.

Edge cases

What if a code is unsupported?

What if a code is unsupported?

Invalid currency is printed.

Are these live exchange rates?

Are these live exchange rates?

No. They are fixed illustrative values in the code.

What's going on

The rates dictionary stores currency units per USD. Dividing by the source rate converts the amount into USD, then multiplying by the destination rate converts outward.

Both codes must be dictionary keys; upper normalizes letter case. These hard-coded rates are teaching values, not current market quotes, and include no fees. The .2f display rounds the example result but does not provide precise financial settlement arithmetic.

What if a code is unsupported? Invalid currency is printed.

Are these live exchange rates? No. They are fixed illustrative values in the code.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why divide by the source rate first?

Pick an answer to check it.

Question 2

What if a code is unsupported?

Pick an answer to check it.

Questions people ask

What if a code is unsupported?

Invalid currency is printed.

Are these live exchange rates?

No. They are fixed illustrative values in the code.

Why divide by the source rate first?

To convert to the common USD base.