2 Sept 2023

Simple Python Currency Converter

In this fun tutorial, we'll show you how to create a cool currency converter app using Python 🐍 and connect it to a currency exchange rate API 🌐. With this app, you can easily change money from one currency to another using the latest exchange rates. 🤑

By the time we're done, you'll have your very own working currency converter app, ready to make money magic happen! 💰✨


Before we get started, make sure you have the following requirements installed on your system

  1. Python (version 3.x or later)
  2. requests library (to make API requests)

You can install the requests library using pip:pip install requests

Set up the Currency Exchange Rate API

For this project, we will use the [ExchangeRate-API](https://www.exchangerate-api.com/), a free and reliable currency exchange rate API. To use the API, sign up for a free account to get an API key.

Once you have the API key, you can access the exchange rate data using the following endpoint


Replace YOUR_API_KEY with your actual API key and SOURCE_CURRENCY_CODE with the three-letter currency code (e.g., USD, EUR, GBP) for the currency you want to convert from.

Create the Currency Converter App

Let's start building our currency converter app. Create a new Python file, e.g., currency_converter.py, and follow along with the code below

import requests
def get_exchange_rate(api_key, source_currency, target_currency):
    url = f"https://v6.exchangerate-api.com/v6/{api_key}/latest/{source_currency}"
    response = requests.get(url)
  data = response.json()
    if data["result"] == "error":
      raise Exception(f"Error: {data['error-type']} - {data['error-info']}")
    exchange_rate = data["conversion_rates"].get(target_currency)
    if not exchange_rate:
      raise Exception(f"Invalid target currency: {target_currency}")
  return exchange_rat
def convert_currency(amount, exchange_rate):
  return amount * exchange_rate
def main():
    api_key = "YOUR_API_KEY"  # Replace with your actual API key
    source_currency = input("Enter the source currency (e.g., USD): ").upper()
    target_currency = input("Enter the target currency (e.g., EUR): ").upper()
  amount = float(input("Enter the amount to convert: "))
        exchange_rate = get_exchange_rate(api_key, source_currency, target_currency)
        converted_amount = convert_currency(amount, exchange_rate)
        print(f"{amount:.2f} {source_currency} is equal to {converted_amount:.2f} {target_currency}")
    except Exception as e:
      print(f"An error occurred: {e}"
if __name__ == "__main__":

Run the Currency Converter App

Save the currency_converter.py file and run it in your terminal or command prompt:python currency_converter.py

The app will prompt you to enter the source currency, target currency, and the amount to convert. After entering the required information, it will fetch the exchange rate from the API and perform the currency conversion, displaying the result.


You made a cool currency converter app in Python! 😎 It talks to a currency exchange rate API to help you convert money between different currencies. 🌍💰 It's super accurate because it grabs the latest exchange rates in real-time. 📈

And guess what? You can make it even cooler! 😃 Add more ways to handle errors, make it look fancy with buttons and stuff (graphical user interface), or put in extra features like looking at old exchange rates or using different APIs. 🛠️📊✨

You've got loads of options to make it your very own awesome currency converter! 🚀👏👍