Python Project: Building a Currency Converter.

Python Project: Building a Currency Converter.

Introduction:


In this project, I built a simple currency converter in Python that reads conversion rates from a .txt file and allows the user to convert an amount from INR to various currencies. This project helped me practice reading files, using dictionaries for fast lookups, and handling user inputs effectively.

These are the steps I took to grasp the concepts:


  1. Prepared the Data File:

    I created a file named CurrencyData.txt that contains currency names and their conversion rates. The file format looks like this:

     Argentine Peso    11.939969    0.083752
     Australian Dollar    0.018288    54.681087
     Bahraini Dinar    0.004461    224.183161
     Botswana Pula    0.161817    6.179806
     Brazilian Real    0.068801    14.534690
     British Pound    0.009453    105.790724
     Bruneian Dollar    0.015993    62.526653
     Bulgarian Lev    0.022144    45.158159
     Canadian Dollar    0.016732    59.765742
     Chilean Peso    11.582991    0.086333........
    
  2. Read the Data:

    Using the open() function, I read all lines from the file and stored them in a dictionary where the key is the currency name and the value is the conversion rate.

  3. Prompted User Input:

    I asked the user to:

    • Enter an amount in INR.

    • Select the currency to convert from the displayed list of available options.

  4. Calculated and Displayed the Conversion:

    Finally, I multiplied the entered amount by the corresponding conversion rate and displayed the result.

  5. Below is the complete code :

These are the problems that I encountered:


  1. Error when the file was missing or not formatted correctly.

  2. Handling non-numeric input for the amount

  3. Invalid user input for currency name.

This is how I solved those problems:


1. Error when the file was missing or not formatted correctly:

Problem: If the file (CurrencyData.txt) was missing or the data inside the file was not in the correct format (for example, missing tabs between currency names and conversion rates), the program wouldn't work as expected.

Solution: You can handle this issue by checking if the file exists before opening it and ensuring its format is correct. Also, you can catch errors like FileNotFoundError if the file is missing.

try:
    with open("CurrencyData.txt") as f:
        lines = f.readlines()
except FileNotFoundError:
    print("The file 'CurrencyData.txt' is missing. Please make sure the file is available.")
except Exception as e:
    print(f"An error occurred while reading the file: {e}")

2. Handling non-numeric input for the amount:

Problem: If the user typed something that was not a number (for example, entered "abc" instead of an amount like "1000"), the program would crash with a ValueError.

Solution: You can prevent this by using a while loop with a try-except block to keep asking the user for a valid number until they enter one.

while True:
    try:
        amount = float(input("Enter the amount: "))  # Convert the input to a number
        break  # Exit the loop if the input is valid
    except ValueError:
        print("Invalid amount. Please enter a numeric value.")  # Handle the error if input is not a number

3. Invalid user input for currency name.

Problem: If the user typed a currency name that wasn’t in the .txt file, the program would give an error, specifically a KeyError.

Solution: You can use a try-except block to catch the KeyError and prompt the user to try again with a valid currency name.

try:
    currency = input("Enter the currency name from above options: ").lower()  # Convert input to lowercase
    result = amount * float(currencyDict[currency])  # Perform the conversion
    print(f"{amount} INR is equal to {result} {currency.upper()}")  # Display result
    break
except KeyError:
    print("Invalid currency name. Please try again by entering a valid option from the list.")

Output when running in the terminal:


Enter the amount: nmb
Invalid amount. Please enter a numeric value.
Enter the amount: 26
Please enter the name of the currency you want to convert to. Available Options are:
ARGENTINE PESO
AUSTRALIAN DOLLAR
BAHRAINI DINAR
BOTSWANA PULA
BRAZILIAN REAL.......
Write/Paste the name from the above options: usd
Invalid currency name. Please try again.
Write/Paste the name from the above options: us dollar
26.0 INR is equal to 0.308438 US DOLLAR

Resources:


My GitHub repository with the project code: GitHub Link

You can take the exchange rate from this Website: Link