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:

How to Create a Currency Converter in Python Picture 1

 

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

How to Create a Currency Converter in Python Picture 2

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:

How to Create a Currency Converter in Python Picture 3

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

4 ★ | 1 Vote

May be interested

  • Currency converter with GoogleCurrency converter with Google
    you guys use google very often, but some of the tricks on google you didn't pay attention to. the feature of converting units and currencies using google is quite a good feature, this feature helps you quickly convert units or convert currencies with goo.
  • How to Create a Dictionary Application in PythonHow to Create a Dictionary Application in Python
    building a dictionary app but rich in functionality using python's tkinter library is not difficult. here are detailed instructions on how to create a dictionary application using python.
  • How to Create a GUI Calendar in PythonHow to Create a GUI Calendar in Python
    this calendar is small but fully functional and you can create it quickly with help from tkinter and python.
  • How to Convert Currency in Microsoft ExcelHow to Convert Currency in Microsoft Excel
    tipsmake today will teach you how to create a currency converter using microsoft excel. if you just need to convert a certain monetary value to another, it's very simple, you can use excel's multiplication formula to apply the conversion rate to the existing data. on windows computers, you can also install the kutools plug-in to convert large amounts of money at up-to-date rates; the process is more advanced but also more precise.
  • More than 100 Python exercises have solutions (sample code)More than 100 Python exercises have solutions (sample code)
    more than 100 python code examples are shared by guy zhiwehu on github, however, the solution of this series is written on the old python version. following tipsmake.com will be vietnameseized and edited to suit python 3.x to help you learn and practice python.
  • Recursive function in PythonRecursive function in Python
    in the previous articles, you learned about python functions, built-in python functions, and user-defined python functions. in this article we will learn more about recursive functions in python, which call itself, as well as how to create recursive functions and examples.
  • Decorator in PythonDecorator in Python
    decorator is used a lot in python. so how to create a decorator and why you should use it. let's go find the answer!
  • Build a color game using Tkinter in PythonBuild a color game using Tkinter in Python
    you can program mini-games in python in a very simple way. here's how to create a color quiz game using tkinter in python.
  • 5 free PDF converter apps for smartphones and tablets5 free PDF converter apps for smartphones and tablets
    can you save the file as a pdf on your phone? probably not. typically, you'll need a laptop or desktop computer to convert documents or images to pdf, if you don't have these apps.
  • WinX HD Video Converter Deluxe - Professional Video ConverterWinX HD Video Converter Deluxe - Professional Video Converter
    winx hd video converter deluxe is your one stop shop for converting video files of almost any type. can't play hevc file? convert them with winx video converter and you can view files on any device.