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.
1# Exchange rates (example)2rates = {3 'USD': 1.0,4 'EUR': 0.85,5 'GBP': 0.73,6 'INR': 82.57}89# Take inputs10amount = float(input("Enter amount: "))11from_currency = input("From currency (USD/EUR/GBP/INR): ").upper()12to_currency = input("To currency (USD/EUR/GBP/INR): ").upper()1314# Convert15if from_currency in rates and to_currency in rates:16 # Convert to USD first, then to target17 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
Uppercase both currency codes.
- 2
Divide by the source rate to obtain the USD base.
- 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.
๐Related Content
- ๐
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- ๐
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- ๐ป
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum