# shout out to some random of reddit import requests def fetch_exchange_rate(api_key, base_currency="USD"): url = f"https://open.er-api.com/v6/latest/{base_currency}" response = requests.get(url, params={"api_key": api_key}) data = response.json() if response.status_code == 200: return data["rates"] else: print(f"Error fetching data: {data['error-type']}") return None api_key = "your_api_key_here" rates = fetch_exchange_rate(api_key) if rates: print("Exchange rates fetched successfully!") else: print("Failed to fetch exchange rates.") def convert_currency(amount, from_currency, to_currency, rates): if from_currency != "USD": amount = amount / rates[from_currency] converted_amount = amount * rates[to_currency] return converted_amount def main(): api_key = "your_api_key_here" rates = fetch_exchange_rate(api_key) if not rates: print("Error fetching exchange rates.") return print("Welcome to the Real-Time Currency Converter!") amount = float(input("Enter the amount to convert: ")) from_currency = input("Enter the source currency (e.g., USD): ").upper() to_currency = input("Enter the target currency (e.g., EUR): ").upper() if from_currency not in rates or to_currency not in rates: print("Invalid currency code.") return converted_amount = convert_currency(amount, from_currency, to_currency, rates) print(f"{amount} {from_currency} is equal to {converted_amount:.2f} {to_currency}") if __name__ == "__main__": main()