I'm trying to make a currency converter and I'm getting this error, what's the reason?
I'm using alphavantage to get the values.
import requests, json
def AlphpaFetcher(from_currency, to_currency, api_key):
alpha_url = r"https://www.alphavantage.co/query?function = CURRENCY_EXCHANGE_RATE"
final_url = alpha_url + "&from_currency =" + from_currency + "&to_currency =" + to_currency + "&apikey =" + api_key
req_ob = requests.get(final_url)
result = req_ob.json()
print("Realtime Currency Exchange Rate for",
result["Realtime Currency Exchange Rate"]
["2. From_Currency Name"], "TO",
result["Realtime Currency Exchange Rate"]
["4. To_Currency Name"], "is",
result["Realtime Currency Exchange Rate"]
['5. Exchange Rate'], to_currency)
if __name__ == "__main__" :
from_currency = "USD"
to_currency = "BRL"
api_key = "XXXXXXXXXX"
AlphpaFetcher(from_currency, to_currency, api_key)
and return this
Traceback (most recent call last):
File "c:\Users\Lucas\Desktop\teste.py", line 37, in <module>
AlphpaFetcher(from_currency, to_currency, api_key)
File "c:\Users\Lucas\Desktop\teste.py", line 19, in AlphpaFetcher
result["Realtime Currency Exchange Rate"]
KeyError: 'Realtime Currency Exchange Rate'
correct your URL construction: (note no spaces before=
)
alpha_url = r"https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE"
final_url = alpha_url + "&from_currency=" + from_currency + "&to_currency=" + to_currency + "&apikey=" + api_key
req_ob = requests.get(final_url)