Table of Contents
This practical guide explains how to create a currency converter in python with clear steps and useful context. It also covers common requirements, potential problems, and the details that can help you get better results.
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:

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

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.com.
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:

You can now use the Open Exchange Rates API to get up-to-date forex rate data. Hope the article is useful to you.
Conclusion
Understanding Python makes it easier to compare options, avoid common mistakes, and apply the information in this guide more effectively. Review the relevant requirements before making changes or choosing a solution.
FAQ
How do you create a currency converter in python?
Follow the steps in this guide in order, confirm the required settings or tools, and verify the result before making additional changes.
Is it safe to create a currency converter in python?
It is generally safe when you follow the recommended instructions, use trusted tools, and avoid changing settings you do not understand.
What should you do if the process does not work?
Recheck the requirements, restart the device or application when appropriate, and review each step for missed settings or compatibility issues.

Reader Comments 0
Sign in with email or Google to join the discussion.