Currency converter using Python

Hey, Internet programmer today we make a currency converter using python. As we know python is a very versatile programming language and used by big tech giants. Anyway, we use fixer API to get the live conversion rates and convert the corresponding amount. So, get your API key from here.

Installation

We need a requests module in order to call that API.

type following command to install it,

pip install requests

Code

import requests
api_url_end = 'http://data.fixer.io/api/latest?access_key=YOUR_API_KEY_HERE'

def currency_convertor(currency_from,currency_to,amount):
    rate = response.json()['rates'][currency_from]
    amount_in_EUR = amount/rate
    result = amount_in_EUR*(response.json()['rates'][currency_to])
    print(result)

response=requests.get(api_url_end)

print('List of country code '+str(response.json()['rates'].keys())+'\n')

convert_from = input('Currency from: ')
convert_to = input('Currency to: ')

amount_to_convert = int(input("Enter the amount to convert: "))

currency_convertor(base_currency,convert_to,amount_to_convert)

# follow on instagram @code_snail

Output

image 7

First, import the requests module then we declare api_url_end which have API endpoint of currency and yes you have to replace YOUR_API_KEY_HERE which you will get after signing up on fixer.io

Before understanding currency_convertor function, we call our API endpoint using requests.get() then we print the country code which the user can easily find.

Then we ask the user to from which currency to which currency they want to convert and then we ask for the amount to convert it and store it in convert_from, convert_to and amount_to_convert respectively

After we calling currency_convertor() function with convert_from, convert_to and amount_to_convert arguments.

We define a function currency_convertor which takes 3 input arguments i.e. currency_from, currency_to, amount.

Then we define rate as a variable that stores the rate of the base currency. Then calculate amount_in_EUR as the base currency for the data received is EUR.
And finally, the result will be the amount_in_EUR multiplied by the rate of currency_to.

Must try this mini-project and share it with your friends. Follow me on Instagram @code_snail

Also, try,

Support CodeSnail

If you appreciate my work, or if it has helped you along your journey. It would mean a lot to me if you could write a message on my wall and share a cup of coffee (or tea) with me.

Buy Me A Coffee

Related Articles

Leave a Comment

Copy link
Powered by Social Snap