Sai A Sai A
Updated date Jan 06, 2024
In this blog, we will learn how to create a currency converter in Python. Explore methods using real-time exchange rates, fixed rates, and interactive user input.

Introduction:

In today's world, understanding and working with different currencies is a valuable skill. Whether you are a traveler, an online shopper, or a developer, having the ability to convert currencies can be quite handy. In this blog, we will explore how to build a simple yet effective currency converter in Python. We will cover multiple methods to achieve the conversion.

Method 1: Using the 'forex-python' Library

Our first method utilizes the 'forex-python' library, as demonstrated in the program above. This method is straightforward, efficient, and suitable for basic currency conversion needs. However, it requires an internet connection to fetch the latest exchange rates.

Let's start by writing a Python program for our currency converter. We'll use the 'forex-python' library, which provides an easy-to-use interface for fetching exchange rates.

# Import the necessary library
from forex_python.converter import CurrencyRates

# Create an instance of the CurrencyRates class
c = CurrencyRates()

def convert_currency(amount, from_currency, to_currency):
    # Fetch the exchange rate
    exchange_rate = c.get_rate(from_currency, to_currency)
    
    # Perform the conversion
    converted_amount = amount * exchange_rate
    
    # Return the result
    return converted_amount

# Example usage
amount_to_convert = 100
from_currency_code = 'USD'
to_currency_code = 'EUR'

result = convert_currency(amount_to_convert, from_currency_code, to_currency_code)

# Print the result
print(f"{amount_to_convert} {from_currency_code} is equal to {result:.2f} {to_currency_code}")

Output:

100 USD is equal to 86.36 EUR

The program utilizes the 'forex-python' library, which simplifies the process of fetching exchange rates. The convert_currency function takes three parameters: amount (the amount to convert), from_currency (the original currency), and to_currency (the target currency). It then calculates the converted amount using the fetched exchange rate and prints the result.

Method 2: Manual Conversion with Fixed Rates

For scenarios where internet access is limited, you can implement a manual conversion method with fixed exchange rates. Create a dictionary with predefined exchange rates and use it for currency conversion. This method is useful when real-time rates are not critical, and you want to avoid frequent API calls.

fixed_rates = {'USD': 1.0, 'EUR': 0.85, 'GBP': 0.73}

def convert_currency_manual(amount, from_currency, to_currency):
    # Fetch the fixed exchange rate
    exchange_rate = fixed_rates.get(to_currency, 1.0) / fixed_rates.get(from_currency, 1.0)
    
    # Perform the conversion
    converted_amount = amount * exchange_rate
    
    # Return the result
    return converted_amount

# Example usage
result_manual = convert_currency_manual(amount_to_convert, from_currency_code, to_currency_code)

# Print the result
print(f"{amount_to_convert} {from_currency_code} is equal to {result_manual:.2f} {to_currency_code}")

Output:

100 USD is equal to 85.00 EUR

Method 3: Interactive User Input

To enhance user interaction, you can modify the program to take user input for the amount and currency codes. This makes the currency converter more dynamic and user-friendly.

amount_to_convert = float(input("Enter the amount to convert: "))
from_currency_code = input("Enter the code of the original currency: ")
to_currency_code = input("Enter the code of the target currency: ")

result = convert_currency(amount_to_convert, from_currency_code, to_currency_code)

# Print the result
print(f"{amount_to_convert} {from_currency_code} is equal to {result:.2f} {to_currency_code}")

Output:

Enter the amount to convert: 150
Enter the code of the original currency: USD
Enter the code of the target currency: EUR
150.0 USD is equal to 129.54 EUR

Conclusion:

In this blog, we have explored the process of building a currency converter in Python. We started with a simple program using the 'forex-python' library, providing a quick and efficient solution for real-time currency conversion. We then introduced two additional methods: manual conversion with fixed rates and interactive user input.

Comments (0)

There are no comments. Be the first to comment!!!