Building a currency converter in Python is one of the most rewarding projects for anyone starting in programming. Beyond being an extremely useful everyday tool, this project teaches you how software communicates with the real world through external data. In this guide, you will learn how to build a Python currency converter step by step, exploring everything from basic logic to connecting with systems that provide real-time exchange rates.
Python is the ideal language for this task due to its clean syntax and the enormous variety of Python libraries available. By building this script, you will not only practice Python programming logic but also learn to handle JSON data in Python, the gold standard for information exchange on the internet today.
Why automate currency conversion?
In the globalized financial market, exchange rates change every second. Tracking those changes manually in spreadsheets is tedious and error-prone. An automated script can fetch the exact value of the Dollar, Euro, or any other currency directly from official sources. By building your own converter, you gain the autonomy to integrate that functionality into larger systems, such as e-commerce platforms, financial dashboards, or messaging bots.
Setting up the development environment
Before writing code, make sure Python is installed. Check the guide on how to install Python if needed. For this project, the requests library is used — the standard tool for making HTTP requests and accessing external data. Install it with:
pip install requestsIf you encounter issues, check that your Python virtual environment is properly configured to avoid conflicts between projects.
How the converter logic works
The logic behind a currency converter needs three pieces of information: the source currency (e.g. USD), the destination currency (e.g. EUR), and the amount to convert. The program then fetches the current exchange rate. If 1 Dollar equals 0.92 Euros and you want to convert 10 Dollars, the calculation is 10 * 0.92 = 9.20. To keep the program professional, hardcoded values are never used — the script fetches data dynamically.
Step 1: Fetching data from the API
The free AwesomeAPI is used here — it provides quotes updated every 30 seconds and requires no complex access keys for basic use.
import requests
def fetch_rate(origin, destination):
url = f"https://economia.awesomeapi.com.br/json/last/{origin}-{destination}"
try:
response = requests.get(url)
if response.status_code == 200:
return response.json()
return None
except Exception as e:
print(f"Error fetching data: {e}")
return NoneThe requests library knocks on the API server’s door and asks for the data. The try-except block prevents the program from crashing if the internet is down or the server is unavailable.
Step 2: Processing the received data
When the API responds, it sends a JSON file that in Python works much like a Python dictionary. The exchange rate value needs to be extracted — typically identified by the 'bid' key, which represents the buying price.
def convert_amount(amount, origin, destination):
data = fetch_rate(origin, destination)
if data:
# The API returns a compound key, e.g. 'USDEUR'
key = f"{origin}{destination}"
rate = float(data[key]['bid'])
converted = amount * rate
return converted, rate
return None, NoneThe rate value is converted to a Python float because data arriving from the internet is typically a string, and mathematical calculations cannot be performed on strings.
Step 3: User interaction
The .upper() method ensures that even if the user types “usd” in lowercase, the program reads it as “USD”, which is the standard required by most financial APIs as defined by ISO 4217.
def start_converter():
print("--- Smart Currency Converter ---")
from_currency = input("Source currency (e.g. USD): ").upper()
to_currency = input("Destination currency (e.g. EUR): ").upper()
try:
amount = float(input(f"How much {from_currency} to convert? "))
result, rate = convert_amount(amount, from_currency, to_currency)
if result:
print(f"Current rate: {rate:.4f}")
print(f"{amount} {from_currency} = {result:.2f} {to_currency}")
else:
print("Could not complete conversion. Check the currency codes.")
except ValueError:
print("Please enter a valid numeric value.")Complete project script
import requests
def fetch_rate(origin, destination):
"""Makes a request to the exchange rate API."""
url = f"https://economia.awesomeapi.com.br/json/last/{origin}-{destination}"
try:
response = requests.get(url)
if response.status_code == 200:
return response.json()
return None
except:
return None
def main():
print("=" * 30)
print(" PYTHON CURRENCY CONVERTER ")
print("=" * 30)
origin = input("Source currency code (e.g. USD, EUR, GBP): ").upper()
destination = input("Destination currency code (e.g. EUR, BRL, JPY): ").upper()
try:
amount = float(input(f"Amount to convert ({origin}): "))
api_data = fetch_rate(origin, destination)
if api_data:
key = f"{origin}{destination}"
rate = float(api_data[key]['bid'])
result = amount * rate
print("n" + "-" * 30)
print(f"Rate: 1 {origin} = {rate:.4f} {destination}")
print(f"Converted: {result:.2f} {destination}")
print("-" * 30)
else:
print("n[Error] Currency pair not found or service unavailable.")
except ValueError:
print("n[Error] Invalid input. Enter numbers only for the amount.")
except Exception as e:
print(f"n[Unexpected error] {e}")
if __name__ == "__main__":
main()Expanding the project
Now that you know how to build a Python currency converter step by step, several features can take the project further. You can create a visual interface using Tkinter graphical interfaces, save a conversion history in CSV files in Python, turn the script into a website with Flask, or set up notifications that alert you when the Dollar drops below a target value.
Frequently Asked Questions
How do I find currency codes?
Currency codes follow the international ISO 4217 standard. The most common ones are USD (US Dollar), EUR (Euro), GBP (British Pound), JPY (Japanese Yen), and BTC (Bitcoin).
Do I need to pay to use the currency API?
This tutorial uses AwesomeAPI, which is free for occasional queries. For systems requiring thousands of queries per second, paid plans exist on services like Open Exchange Rates.
Does the program work without internet?
No. Since exchange rates change constantly, the script needs to connect to an external server to fetch real-time data.
Can I convert cryptocurrencies with this code?
Yes. The API used supports Bitcoin (BTC), Ethereum (ETH), and Litecoin (LTC) quotations against various fiat currencies.
What does ‘bid’ mean in the API response?
‘Bid’ refers to the buying price of the currency in the financial market — the value the market is willing to pay for that currency at that exact moment.
How do I turn this script into an executable (.exe)?
Use PyInstaller to package your code and all libraries into a single executable file for Windows.






