top of page

Currency Calculator


What is a Currency Calculator?

A Currency Calculator is an application that allows users to convert one currency to another. Users can specify the amount they want to convert and the target currency to perform the conversion based on the current exchange rate. Such applications are highly useful for people who follow the foreign exchange markets and make transactions between different currencies.


Project Objectives and Student Benefits

Through this project, students will reinforce their ability to develop simple and functional programs using basic Python knowledge. Additionally, they will gain experience in user interaction and data processing.


Project Outputs

  • Create a currency calculator application using basic Python structures and control flows.

  • Collect data from users and manage currency conversion operations by processing this data.

  • Write more modular and readable code using functions.


Step-by-Step Tasks of the Project

  1. Define exchange rates.

  2. Collect the amount to be converted and the target currency from the user.

  3. Perform the currency conversion and calculate the result.

  4. Print the result.

  5. Make the code more modular using functions.


Solution Without Functions


Step 1: Define Exchange Rates

In the first step, exchange rates will be defined.

# Define exchange rates
exchange_rates = {
    "USD": 1.0,  # USD is used as the reference
    "EUR": 0.85,
    "GBP": 0.75,
    "JPY": 110.0,
    "TRY": 8.5
}

This code snippet defines the exchange rates. USD is used as the reference, and the values of other currencies are specified in terms of USD.


Step 2: Collect the Amount to be Converted and the Target Currency from the User

In the second step, the amount to be converted and the target currency will be collected from the user.

# Collect the amount to be converted and the target currency from the user
amount = float(input("Enter the amount you want to convert: "))
target_currency = input("Enter the target currency (USD, EUR, GBP, JPY, TRY): ").upper()

This code snippet collects the amount to be converted and the target currency from the user.


Step 3: Perform the Currency Conversion and Calculate the Result

In the third step, the currency conversion is performed, and the result is calculated.

# Perform the currency conversion and calculate the result
if target_currency in exchange_rates:
    result = amount * exchange_rates[target_currency]
else:
    result = "Invalid currency"

This code snippet performs the currency conversion and calculates the result. If the target currency is invalid, it returns an error message.


Step 4: Print the Result

In the fourth step, the calculated result is printed.

# Print the result
print("Result:", result)

This code snippet prints the calculated result.


Solution with Functions


Step 1: Function to Define Exchange Rates

In the first step, exchange rates will be defined.

def define_exchange_rates():
    """Defines exchange rates."""
    return {
        "USD": 1.0,  # USD is used as the reference
        "EUR": 0.85,
        "GBP": 0.75,
        "JPY": 110.0,
        "TRY": 8.5
    }

This function defines and returns the exchange rates.


Step 2: Function to Collect the Amount to be Converted and the Target Currency from the User

In the second step, the amount to be converted and the target currency will be collected from the user.

def collect_data():
    """Collects the amount to be converted and the target currency from the user."""
    amount = float(input("Enter the amount you want to convert: "))
    target_currency = input("Enter the target currency (USD, EUR, GBP, JPY, TRY): ").upper()
    return amount, target_currency

This function collects and returns the amount to be converted and the target currency from the user.


Step 3: Function to Perform the Currency Conversion and Calculate the Result

In the third step, the currency conversion is performed, and the result is calculated.

def perform_conversion(exchange_rates, amount, target_currency):
    """Performs the currency conversion and calculates the result."""
    if target_currency in exchange_rates:
        return amount * exchange_rates[target_currency]
    else:
        return "Invalid currency"

This function performs the currency conversion and calculates the result.


Step 4: Function to Print the Result

In the fourth step, the calculated result is printed.

def print_result(result):
    """Prints the calculated result."""
    print("Result:", result)

This function prints the calculated result.


Step 5: Function to Start the Currency Calculator Application

This function starts the Currency Calculator application and manages its flow.

def start_calculator():
    """Starts the Currency Calculator application."""
    exchange_rates = define_exchange_rates()
    amount, target_currency = collect_data()
    result = perform_conversion(exchange_rates, amount, target_currency)
    print_result(result)

# Starting the application
start_calculator()

This function starts the Currency Calculator application and manages its flow. It collects the necessary data from the user, performs the currency conversion, and prints the result.


Concepts Used

  • Data Types: In this project, numbers (float, int) and strings (user input) are used.

  • Conditional Statements: if-else structures are used to perform the currency conversion and determine the correct operation.

  • Functions: Functions make the code modular and reusable.

  • Dictionaries: Exchange rates are stored in a dictionary.

bottom of page