Skip to content

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.

command
C:\Users\Your Name>pip install requests
C:\Users\Your Name>pip install beautifulsoup4
command
C:\Users\Your Name>pip install requests
C:\Users\Your Name>pip install beautifulsoup4

Getting Started

Create a Project

  1. Create a folder named CurrencyConverterCurrencyConverter.
  2. Open the folder in your code editor or IDE.
  3. Create a file named currencyconverter.pycurrencyconverter.py.
  4. Copy the code given code and paste it in your currencyconverter.pycurrencyconverter.py file.

Write the Code

  1. Copy and paste the following code in your currencyconverter.pycurrencyconverter.py file.
⚙️ Currency Converter
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
# 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}') 
  1. Save the file.
  2. Open the terminal in your code editor or IDE and navigate to the folder CurrencyConverterCurrencyConverter.
command
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
command
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
  1. You can see the output in the terminal.

Explanation

  1. First, we have imported the required modules. We have imported the requestsrequests module and BeautifulSoupBeautifulSoup module. We have imported the requestsrequests module to request the URL and the BeautifulSoupBeautifulSoup module to parse the HTML.
currencyconverter.py
import requests
from bs4 import BeautifulSoup
currencyconverter.py
import requests
from bs4 import BeautifulSoup
  1. Then we have created a variable named urlurl and assigned the URL to it. We have used %s%s to add the variables in the URL.
currencyconverter.py
url = "https://www.x-rates.com/calculator/?from=%s&to=%s&amount=%s"
currencyconverter.py
url = "https://www.x-rates.com/calculator/?from=%s&to=%s&amount=%s"
  1. Then we have printed the list of currencies. We have used the print()print() function to print the list of currencies.
currencyconverter.py
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.
    ''')
currencyconverter.py
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.
    ''')
  1. Then we have used the input()input() function to get the user input. We have used the upper()upper() function to convert the user input to uppercase. We have used the input()input() function to get the user input. We have used the upper()upper() function to convert the user input to uppercase.
currencyconverter.py
from_currency = input("From Currency: ").upper()
to_currency = input("To Currency: ").upper()
amount = input("Amount: ")
currencyconverter.py
from_currency = input("From Currency: ").upper()
to_currency = input("To Currency: ").upper()
amount = input("Amount: ")
  1. Then we have requested the URL. We have used the get()get() function to request the URL. We have used the find()find() function to find the converted amount. We have used the texttext attribute to get the text from the HTML. We have used the find()find() function to find the converted amount. We have used the texttext attribute to get the text from the HTML.
currencyconverter.py
response = requests.get(url % (from_currency, to_currency, amount))
soup = BeautifulSoup(response.text, "html.parser")
converted_amount = soup.find("span", class_="ccOutputRslt").text
currencyconverter.py
response = requests.get(url % (from_currency, to_currency, amount))
soup = BeautifulSoup(response.text, "html.parser")
converted_amount = soup.find("span", class_="ccOutputRslt").text
  1. Then we have printed the converted amount. We have used the print()print() function to print the converted amount.
currencyconverter.py
print(f'{amount} {from_currency} = {converted_amount}')
currencyconverter.py
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 requestsrequests module and BeautifulSoupBeautifulSoup 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