c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
Python

How to create a currency convertor in python

| | python

In this tutorial I will show you how to create a currency convertor using python program.

Install required Package:

pip install python-dateutil requests BeautifulSoup4

PYTHON CODE:

import requests

from bs4 import BeautifulSoup as bs
import re
from dateutil.parser import parse

def convert_currency_xe(src, dst, amount):
url = f"https://www.xe.com/currencyconverter/convert/?Amount={amount}&From={src}&To={dst}"
content = requests.get(url).content
soup = bs(content, "html.parser")
exchange_rate_html = soup.find_all("p")[2]
last_updated_datetime = parse(re.search(r"Last updated (.+)", exchange_rate_html.parent.parent.find_all("div")[-2].text).group()[12:])
return last_updated_datetime, exchange_rate_html.text

if __name__ == "__main__":
from_currency = input("From Currency: ").upper()
to_currency = input("To Currency: ").upper()
amount = int(input("Enter the amount: "))

last_updated_datetime, exchange_rate = convert_currency_xe(from_currency, to_currency, amount)
print("Last updated datetime:", last_updated_datetime)
print(f"{amount} {from_currency} = {exchange_rate} {to_currency}")

OUTPUT:

Currency convertor using python

VIDEO GUIDE: