How to Create a Currency Converter in Python

Real-time currency conversion with an extremely simple Python script. Here are detailed instructions on how to create a currency converter in Python.

Open Exchange Rates is a data provider containing information about currency exchange rates around the world. They have an API that you can use inside the app. It helps you to send requests for exchange rate data for different currencies. This information is regularly updated.

You can use the API in your Python script to get exchange rate information and convert currencies.

How to get API tokens from Open Exchange Rates

To use the API in a Python script, you need an access token. You can extract this token by logging into your account and creating a new token.

1. Sign in or create a new account on the Open Exchange Rates web. Click Free Plan to create a free account:

Picture 1 of How to Create a Currency Converter in Python

 

2. In your account, use the menu on the left to navigate to the App IDs page :

Picture 2 of How to Create a Currency Converter in Python

3. By default, you already have an App ID created on the page. Copy and save it for later as this is the access token needed to communicate with the API.

How to use token API to get exchange rate data

You can use tokens in a Python script to make queries to the Open Exchange Rates API. If you're new to it, you can learn more about using Python on TipsMake.

1. Create a new file named currency-exchange-converter.py and enter the request module at the top. This allows you to send queries to the API.

import requests

2. Store the App ID you saved from your Open Exchange Rates account:

api_key = "App ID của bạn"

3. Store the endpoint used to get the exchange rate information. Include an API key to authenticate your access to the API:

url = f"https://openexchangerates.org/api/latest.json?app_id={api_key}"

4. Create a query for conversion rate data and save the response in JSON format:

response = requests.get(url) data = response.json()

5. Inside this response include the exchange rate, in the format {'currency': 'rate'}. For example, {'AED': 3.67286, 'AFN': 88.978998, 'ALL': 108.421111}. They are information related to the base rate, default in USD. For example, 1 USD equals about 3.67 AED. Save this conversion rate data:

exchange_rates = data["rates"]

6. Print the currencies available to the user:

available_currencies = "" for currency in exchange_rates.keys():   available_currencies += currency + ", " # Xóa dấu phẩy và khoảng trắng ở cuối available_currencies = available_currencies[:-2] print("Available currencies: " + available_currencies)

 

How to convert the required number of users

Use exchange rate data to convert an amount of money from one unit to another.

1. Ask the user to enter their principal and the currency they want to convert:

from_currency = input("Nhập tiền tệ gốc: ").upper() to_currency = input("Nhập tiền tệ mục tiêu: ").upper()

2. Ask the user to enter the amount of coins they want to convert:

amount = float(input("Nhập số lượng muốn chuyển đổi: "))

3. Calculate the amount for the new currency:

original_amount = amount / exchange_rates[from_currency] converted_amount = original_amount * exchange_rates[to_currency]

4. Print the result:

print(f"{amount} {from_currency} = {converted_amount} {to_currency}")

How to run a Python script

You can run Python scripts using a command line or terminal.

1. In a command line, navigate to the location where you saved the Python file. If it was a desktop, your command would look like this:

cd C:UsersSharlDesktop

2. Install the requests module so that the Python script can recognize it.

pip install requests

3. Run Python script with python command :

python currency-exchange-converter.py

4. Enter your amount, followed by the currency you want to convert.

5. Enter the amount of money you want to exchange. This script will display the converted quantity as follows:

Picture 3 of How to Create a Currency Converter in Python

You can now use the Open Exchange Rates API to get up-to-date forex rate data. Hope the article is useful to you.

Update 11 March 2023
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile