Currency Converter in Python
About the project: Creating a Python project for a currency converter.
This program will convert an amount from one currency to another using a simple, predefined set of exchange rates.
It will be a console-based application that allows you to choose the currencies and enter the amount you want to convert.
This program uses a dictionary to store a set of predefined exchange rates. You can easily add more currencies and update the rates as needed to keep the converter current.
How to use this Currency Converter:
- Run the Python script.
- The program will display a list of available currencies.
- It will then prompt you to enter the currency to convert from, the currency to convert to, and the amount.
- The script will perform the calculation and display the converted amount.
- You can type q at the currency input prompt to quit the program.
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# currency_converter.py
def get_numeric_input(prompt, min_value=0):
"""
Helper function to get a valid positive numeric input from the user.
"""
while True:
try:
value = float(input(prompt).strip())
if value <= min_value:
print("Please enter a positive value.")
else:
return value
except ValueError:
print("Invalid input. Please enter a number.")
def convert_currency(amount, from_currency, to_currency, rates):
"""
Converts an amount from a source currency to a target currency.
"""
if from_currency == to_currency:
return amount
# Check if currencies are in the rate dictionary
if from_currency not in rates or to_currency not in rates:
return None
# Convert to a base currency (e.g., USD) first, then to the target currency.
# This makes the conversion logic simpler and more scalable.
amount_in_usd = amount / rates[from_currency]
converted_amount = amount_in_usd * rates[to_currency]
return converted_amount
def main():
"""
Main function to run the Currency Converter.
"""
print("--- Python Currency Converter ---")
# Define a dictionary of exchange rates relative to a base currency (e.g., USD)
# You can update these values to reflect current rates.
exchange_rates = {
"usd": 1.0, # US Dollar (base)
"eur": 0.93, # Euro
"gbp": 0.81, # British Pound
"jpy": 156.45, # Japanese Yen
"cad": 1.37, # Canadian Dollar
"aud": 1.51, # Australian Dollar
"inr": 83.39 # Indian Rupee
}
# Print a list of available currencies
print("\nAvailable currencies:")
for currency in exchange_rates.keys():
print(f"- {currency.upper()}")
while True:
# Get user input for currencies and amount
from_currency = input("\nConvert from currency (e.g., USD): ").strip().lower()
if from_currency == 'q':
print("Exiting Currency Converter. Goodbye!")
break
if from_currency not in exchange_rates:
print("Invalid source currency. Please choose from the available list.")
continue
to_currency = input("Convert to currency (e.g., EUR): ").strip().lower()
if to_currency not in exchange_rates:
print("Invalid target currency. Please choose from the available list.")
continue
amount = get_numeric_input(f"Enter amount in {from_currency.upper()}: ")
# Perform the conversion
converted_amount = convert_currency(amount, from_currency, to_currency, exchange_rates)
if converted_amount is not None:
print(f"\n{amount:.2f} {from_currency.upper()} is equal to {converted_amount:.2f} {to_currency.upper()}.")
else:
print("Conversion failed. Please check your currency inputs.")
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()
