Simple Currency Converter

Convert between currencies with exchange rates.

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

What's going on

Convert using exchange rates.

Key Concepts:

Convert to base currency (USD)
Then convert to target currency
Use exchange rates