Hi everyone,
Welcome back to my blog. In this post, I will show you how to code a very simple currency converter on python.
This is another simple project on python, that could help improve your python programming skills.
Lets get started:
Line 1:
!pip install forex_python
Firstly, we will need to install the forex package in our systems. By using pip we can install it easily.
Line 2 & 3:
from forex_python.converter import CurrencyRates
c = CurrencyRates()
Here, we are importing the currency rates from the forex package so it could be used to do the calculations for exchange rates.
Line 4, 5 & 6:
amount = int(input("Enter the amount: "))
from_currency = input("From Currency: ").upper()
to_currency = input("To Currency: ").upper()
These lines of code, asks the user to input the amount they want to convert, from what currency and to what currency. Using upper() helps us to change the input of the user to upper cases.
Line 7:
print(from_currency, " To ", to_currency, amount)
Line 7, we are asking the computer to print a line saying the currencies we are trying to convert and the amount.
Line 8 & 9:
result = c.convert(from_currency, to_currency, amount)
print(result)
Here, we are using c.convert to do the calculations for the conversion. The last line will print the result.
All the code combined:
!pip install forex_python
from forex_python.converter import CurrencyRates
c = CurrencyRates()
amount = int(input("Enter the amount: "))
from_currency = input("From Currency: ").upper()
to_currency = input("To Currency: ").upper()
print(from_currency, " To ", to_currency, amount)
result = c.convert(from_currency, to_currency, amount)
print(result)
Doing some currency conversion
When running the code, this is what you will see. The user will need to enter the amount, from what currency and to what currency:
In this example, I converted 100 GBP to EUR.
In the second example, I converted 100 EUR to USD.
Congratulations
In this tutorial, you learned how to code your own currency converter using Python in 9 lines of code.
I hope you enjoyed this tutorial. Please make sure to see my other contents :).
Click on the link below to check my video on YouTube on how to code this on Jupyter Notebook. youtu.be/pvp_Q_BwODY