# Python Currency Converter

This utility converts amounts between different currencies using real-time exchange rates provided by the `forex-python` library. It also handles currency symbols for better readability.

**Modules Used:**
*   `forex-python`: To fetch exchange rates and perform conversions.
*   [[programming/python/modules/argparse-module|argparse]]: To handle command-line arguments.

## Installation

```bash
pip install forex-python
```

## The Code

Save this as `converter.py`.

```python
from forex_python.converter import CurrencyRates, CurrencyCodes
import argparse

def convert_currency(amount, from_currency, to_currency):
    c = CurrencyRates()
    codes = CurrencyCodes()

    try:
        print(f"Converting {amount} {from_currency} to {to_currency}...")
        
        # Perform conversion
        # Note: This requires an active internet connection
        converted_amount = c.convert(from_currency, to_currency, amount)
        rate = c.get_rate(from_currency, to_currency)
        
        # Get currency symbol (e.g., $, €)
        to_symbol = codes.get_symbol(to_currency) or to_currency
        
        print(f"Rate: 1 {from_currency} = {rate:.4f} {to_currency}")
        print(f"Result: {to_symbol}{converted_amount:.2f}")

    except Exception as e:
        print(f"Error: {e}")
        print("Ensure you have an internet connection and valid currency codes (e.g., USD, EUR).")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Currency Converter CLI")
    parser.add_argument("amount", type=float, help="Amount to convert")
    parser.add_argument("from_currency", help="Source currency code (e.g., USD)")
    parser.add_argument("to_currency", help="Target currency code (e.g., EUR)")
    
    args = parser.parse_args()
    
    convert_currency(args.amount, args.from_currency.upper(), args.to_currency.upper())
```

## Usage

```bash
# Convert 100 USD to EUR
python converter.py 100 USD EUR

# Convert 5000 JPY to USD
python converter.py 5000 JPY USD
```

[[programming/python/python]]