How to create a currency converter in python
In this blog we are going to see how to create a currency converter in python.
You can also see How to Convert Emoji to Text in python, How to Create a barcode in python, Convert PDF files to Doc in python, How to create URL Shortner with Python ?
In Python, with just a few lines of code, you can write a program that converts currency into another currency using up–to–date exchange rates. First install the forex library using:
pip install forex-python
The code below will convert any currency. You just have to know the currency codes of the currencies you are about to work with. Try it out.
from forex_python.converter import CurrencyRates # Instantiate the converter
converter = CurrencyRates()
def currency_converter():
# Enter the amount to convert
amount = int(input("Please enter amount to convert: "))
# currency code of the amount to convert
from_currency = input("Enter the currency code of "
"amount you are converting : ").upper()
# currency code of the amount to convert to
to_currency = input("Enter the currency code you "
"are converting to: ").upper()
# convert the amount
converted_amount = converter.convert(from_currency,
to_currency, amount)
return f' The amount is {converted_amount:.2f} and ' \
f'the currency is {to_currency}'
print(currency_converter())