Certainly! Here's a simplified example of a currency
converter script in Python:
Copy code
import requests
def convert_currency(amount, from_currency, to_currency):
"""
Converts an amount
from one currency to another.
Args:
amount
(float): The amount to be converted.
from_currency
(str): The currency code of the original currency.
to_currency
(str): The currency code of the desired currency.
Returns:
float: The
converted amount.
"""
url =
f"https://api.exchangerate-api.com/v4/latest/{from_currency}"
response =
requests.get(url)
data =
response.json()
conversion_rate =
data['rates'][to_currency]
converted_amount =
amount * conversion_rate
return
converted_amount
# Example usage
amount = 100.0 #
Amount to be converted
from_currency = "USD" # Original currency code
to_currency = "EUR" # Desired currency code
converted_amount = convert_currency(amount, from_currency,
to_currency)
print(f"The converted amount is: {converted_amount}
{to_currency}")
In this script, the convert_currency() function takes an
amount, the currency code of the original currency, and the currency code of
the desired currency as input. It uses an API (in this case,
exchangerate-api.com) to fetch the latest exchange rates and performs the
conversion based on the retrieved data. The function returns the converted
amount.
The example usage section demonstrates how to use the
function by providing a sample amount, original currency code, and desired
currency code. It then prints the converted amount with the desired currency
code.
Please note that this is a basic example and doesn't handle
all possible scenarios, such as error handling or extensive error checking. In
a production environment, you may need to enhance the script with additional
features and error handling based on your specific requirements.
|