Currency Converter
Abstract
Currency Converter is a simple python project that converts the currency from one country to another. This a command-line project that is very easy to use and understand. This project is a very simple project that makes a user-friendly interface. This project is very helpful for beginners who want to learn to code in python. This project is very easy to operate and understood by the users. You can access 20 different currencies in this project. You can convert any currency to any currency.
Prerequisites
- Python 3.6 or above
- A code editor or IDE
- Request module
- BeautifulSoup module
Before you Start
Before starting making this project, you must have python installed on your computer. If you don’t have python installed you can download it from here. You must have a code editor or IDE installed on your computer. If you don’t have any code editor or IDE installed you can download Visual Studio Code from here. You must have the request module and BeautifulSoup module installed on your computer. If you don’t have these modules installed you can install them by using the following commands in your terminal.
C:\Users\Your Name>pip install requests
C:\Users\Your Name>pip install beautifulsoup4
C:\Users\Your Name>pip install requests
C:\Users\Your Name>pip install beautifulsoup4
Getting Started
Create a Project
- Create a folder named
CurrencyConverter
CurrencyConverter
. - Open the folder in your code editor or IDE.
- Create a file named
currencyconverter.py
currencyconverter.py
. - Copy the code given code and paste it in your
currencyconverter.py
currencyconverter.py
file.
Write the Code
- Copy and paste the following code in your
currencyconverter.py
currencyconverter.py
file.
⚙️ Currency Converter
# Currency Converter
# Importing the required modules
import requests
from bs4 import BeautifulSoup
# URL
url = "https://www.x-rates.com/calculator/?from=%s&to=%s&amount=%s"
# Getting the user input
print("Currency Converter")
print('''
List of Currencies:
1. USD - US Dollar
2. EUR - Euro
3. GBP - British Pound
4. INR - Indian Rupee
5. AUD - Australian Dollar
6. CAD - Canadian Dollar
7. SGD - Singapore Dollar
8. CHF - Swiss Franc
9. MYR - Malaysian Ringgit
10. JPY - Japanese Yen
11. CNY - Chinese Yuan Renminbi
12. NZD - New Zealand Dollar
13. THB - Thai Baht
14. HUF - Hungarian Forint
15. AED - Emirati Dirham
16. HKD - Hong Kong Dollar
17. MXN - Mexican Peso
18. ZAR - South African Rand
19. PHP - Philippine Peso
20. SEK - Swedish Krona
Don't Enter the Number. Enter the currency code.
''')
from_currency = input("From Currency: ").upper()
to_currency = input("To Currency: ").upper()
amount = input("Amount: ")
# Requesting the URL
response = requests.get(url % (from_currency, to_currency, amount))
soup = BeautifulSoup(response.text, "html.parser")
# Finding the converted amount
converted_amount = soup.find("span", class_="ccOutputRslt").text
# Printing the converted amount
print(f'{amount} {from_currency} = {converted_amount}')
# Currency Converter
# Importing the required modules
import requests
from bs4 import BeautifulSoup
# URL
url = "https://www.x-rates.com/calculator/?from=%s&to=%s&amount=%s"
# Getting the user input
print("Currency Converter")
print('''
List of Currencies:
1. USD - US Dollar
2. EUR - Euro
3. GBP - British Pound
4. INR - Indian Rupee
5. AUD - Australian Dollar
6. CAD - Canadian Dollar
7. SGD - Singapore Dollar
8. CHF - Swiss Franc
9. MYR - Malaysian Ringgit
10. JPY - Japanese Yen
11. CNY - Chinese Yuan Renminbi
12. NZD - New Zealand Dollar
13. THB - Thai Baht
14. HUF - Hungarian Forint
15. AED - Emirati Dirham
16. HKD - Hong Kong Dollar
17. MXN - Mexican Peso
18. ZAR - South African Rand
19. PHP - Philippine Peso
20. SEK - Swedish Krona
Don't Enter the Number. Enter the currency code.
''')
from_currency = input("From Currency: ").upper()
to_currency = input("To Currency: ").upper()
amount = input("Amount: ")
# Requesting the URL
response = requests.get(url % (from_currency, to_currency, amount))
soup = BeautifulSoup(response.text, "html.parser")
# Finding the converted amount
converted_amount = soup.find("span", class_="ccOutputRslt").text
# Printing the converted amount
print(f'{amount} {from_currency} = {converted_amount}')
- Save the file.
- Open the terminal in your code editor or IDE and navigate to the folder
CurrencyConverter
CurrencyConverter
.
C:\Users\Your Name\CurrencyConverter> python currencyconverter.py
Currency Converter
List of Currencies:
1. USD - US Dollar
2. EUR - Euro
3. GBP - British Pound
4. INR - Indian Rupee
5. AUD - Australian Dollar
6. CAD - Canadian Dollar
7. SGD - Singapore Dollar
8. CHF - Swiss Franc
9. MYR - Malaysian Ringgit
10. JPY - Japanese Yen
11. CNY - Chinese Yuan Renminbi
12. NZD - New Zealand Dollar
13. THB - Thai Baht
14. HUF - Hungarian Forint
15. AED - Emirati Dirham
16. HKD - Hong Kong Dollar
17. MXN - Mexican Peso
18. ZAR - South African Rand
19. PHP - Philippine Peso
20. SEK - Swedish Krona
Don't Enter the Number. Enter the currency code.
From Currency: USD
To Currency: INR
Amount: 100
100 USD = 8,309.928657 INR
C:\Users\Your Name\CurrencyConverter> python currencyconverter.py
Currency Converter
List of Currencies:
1. USD - US Dollar
2. EUR - Euro
3. GBP - British Pound
4. INR - Indian Rupee
5. AUD - Australian Dollar
6. CAD - Canadian Dollar
7. SGD - Singapore Dollar
8. CHF - Swiss Franc
9. MYR - Malaysian Ringgit
10. JPY - Japanese Yen
11. CNY - Chinese Yuan Renminbi
12. NZD - New Zealand Dollar
13. THB - Thai Baht
14. HUF - Hungarian Forint
15. AED - Emirati Dirham
16. HKD - Hong Kong Dollar
17. MXN - Mexican Peso
18. ZAR - South African Rand
19. PHP - Philippine Peso
20. SEK - Swedish Krona
Don't Enter the Number. Enter the currency code.
From Currency: USD
To Currency: INR
Amount: 100
100 USD = 8,309.928657 INR
- You can see the output in the terminal.
Explanation
- First, we have imported the required modules. We have imported the
requests
requests
module andBeautifulSoup
BeautifulSoup
module. We have imported therequests
requests
module to request the URL and theBeautifulSoup
BeautifulSoup
module to parse the HTML.
import requests
from bs4 import BeautifulSoup
import requests
from bs4 import BeautifulSoup
- Then we have created a variable named
url
url
and assigned the URL to it. We have used%s
%s
to add the variables in the URL.
url = "https://www.x-rates.com/calculator/?from=%s&to=%s&amount=%s"
url = "https://www.x-rates.com/calculator/?from=%s&to=%s&amount=%s"
- Then we have printed the list of currencies. We have used the
print()
print()
function to print the list of currencies.
print("Currency Converter")
print('''
List of Currencies:
1. USD - US Dollar
2. EUR - Euro
3. GBP - British Pound
4. INR - Indian Rupee
5. AUD - Australian Dollar
6. CAD - Canadian Dollar
7. SGD - Singapore Dollar
8. CHF - Swiss Franc
9. MYR - Malaysian Ringgit
10. JPY - Japanese Yen
11. CNY - Chinese Yuan Renminbi
12. NZD - New Zealand Dollar
13. THB - Thai Baht
14. HUF - Hungarian Forint
15. AED - Emirati Dirham
16. HKD - Hong Kong Dollar
17. MXN - Mexican Peso
18. ZAR - South African Rand
19. PHP - Philippine Peso
20. SEK - Swedish Krona
Don't Enter the Number. Enter the currency code.
''')
print("Currency Converter")
print('''
List of Currencies:
1. USD - US Dollar
2. EUR - Euro
3. GBP - British Pound
4. INR - Indian Rupee
5. AUD - Australian Dollar
6. CAD - Canadian Dollar
7. SGD - Singapore Dollar
8. CHF - Swiss Franc
9. MYR - Malaysian Ringgit
10. JPY - Japanese Yen
11. CNY - Chinese Yuan Renminbi
12. NZD - New Zealand Dollar
13. THB - Thai Baht
14. HUF - Hungarian Forint
15. AED - Emirati Dirham
16. HKD - Hong Kong Dollar
17. MXN - Mexican Peso
18. ZAR - South African Rand
19. PHP - Philippine Peso
20. SEK - Swedish Krona
Don't Enter the Number. Enter the currency code.
''')
- Then we have used the
input()
input()
function to get the user input. We have used theupper()
upper()
function to convert the user input to uppercase. We have used theinput()
input()
function to get the user input. We have used theupper()
upper()
function to convert the user input to uppercase.
from_currency = input("From Currency: ").upper()
to_currency = input("To Currency: ").upper()
amount = input("Amount: ")
from_currency = input("From Currency: ").upper()
to_currency = input("To Currency: ").upper()
amount = input("Amount: ")
- Then we have requested the URL. We have used the
get()
get()
function to request the URL. We have used thefind()
find()
function to find the converted amount. We have used thetext
text
attribute to get the text from the HTML. We have used thefind()
find()
function to find the converted amount. We have used thetext
text
attribute to get the text from the HTML.
response = requests.get(url % (from_currency, to_currency, amount))
soup = BeautifulSoup(response.text, "html.parser")
converted_amount = soup.find("span", class_="ccOutputRslt").text
response = requests.get(url % (from_currency, to_currency, amount))
soup = BeautifulSoup(response.text, "html.parser")
converted_amount = soup.find("span", class_="ccOutputRslt").text
- Then we have printed the converted amount. We have used the
print()
print()
function to print the converted amount.
print(f'{amount} {from_currency} = {converted_amount}')
print(f'{amount} {from_currency} = {converted_amount}')
Next Steps
Congratulations! 🎉 You have successfully created a Currency Converter in Python.
There are many ways to improve this program. Here are some ideas:
- Add more currencies.
- Add a GUI to this program.
- Add a database to this program.
- Add a history feature to this program.
- Add a feature to this program to convert the currency of a country to all the other currencies.
- Add a feature to this program to convert the currency of all the countries to the currency of a country.
Conclusion
In this project, we have learned how to create a Currency Converter in Python. We have learned how to use the requests
requests
module and BeautifulSoup
BeautifulSoup
module. We have learned how to request the URL and parse the HTML. We have learned how to get the user input and convert it to uppercase. We have learned how to print the converted amount. We have learned how to create a Currency Converter in Python. To find more projects like this you can visit Python Central Hub.
Was this page helpful?
Let us know how we did