Currency Converter in Python

Hello Everyone. I hope you are reading this post in good health. This Post is About Making Currency Converter in Python.

Currency Converter in Python

In this Post, We will Learn how to create Currency Converter in Python without using any 3rd party modules. Instead We will use requests library of python which comes with python as prebuilt library. We will Request some APIs to fetch live currency rates and then will perform Currency conversion.

Modules Required

import requests

API for Currency Converter

We will be using Exchangerate-API for currency conversion in python. It Provides us latest rates with base currency USD.

API Link : https://api.exchangerate-api.com/v4/latest/usd

API Gives us result in Json Format. and we can easily convert that to our dictionary type in Python, where currency name is our key and value is numeric.

Code

import requests
base = 'USD'
url = 'https://api.exchangerate-api.com/v4/latest/'+base
currencies = requests.get(url).json()['rates']
amount = 100
from_c='EUR'
to_c = 'PKR'

if from_c!=base:
 amt = amount/currencies[from_c]
output = round(amt *currencies[to_c],2)
print(str(amount)+" "+ from_c + " = "+ str(output)+" "+to_c)

If from Currency is not same as Base Currency Unit, then we first need to convert our amount to equivalent base currency and then we can perform conversion by simple multiplication.

You May Also Like : Image to Text in Python

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *