xxxxxxxxxx
import requests
amount = 18
base_currency = 'USD'
target_currency = 'PHP'
# Make a request to the API to get the latest exchange rates
response = requests.get(f"https://api.exchangerate-api.com/v4/latest/{base_currency}")
data = response.json()
# Extract the exchange rate for the target currency
exchange_rate = data['rates'][target_currency]
# Perform the conversion
converted_amount = amount * exchange_rate
# Print the result
print(f"{amount} {base_currency} is equal to {converted_amount} {target_currency}")
xxxxxxxxxx
# Importing required libraries
import requests
# Function to convert USD to PHP
def usd_to_php(usd_amount):
conversion_url = "https://api.exchangerate-api.com/v4/latest/USD"
try:
response = requests.get(conversion_url)
data = response.json()
php_rate = data['rates']['PHP']
php_amount = usd_amount * php_rate
return php_amount
except requests.exceptions.RequestException:
print("Error occurred during API request.")
return None
# Converting 30 USD to PHP
usd_amount = 30
php_amount = usd_to_php(usd_amount)
# Printing the result
if php_amount:
print(f"{usd_amount} USD is approximately {php_amount:.2f} PHP.")