python-3.xapinameerrorfixer.io

How to solve API key error in GET parameters?


I am getting error in Lecture 4 - CS50's Web Programming with Python and JavaScript 2018 I want to figure it out but I don't know how to My code is:

import requests

base = input("First currency: ")
other = input("Second currency: ")

res = requests.get("http://data.fixer.io/api/latest",params={"access_key":a8fc1a37ad16c61174a1f0395381ae41, "base": base, "symbols": other})
if res.status_code != 200:
    raise Exception("ERROR: API request unsuccessful.")

data = res.json()
print(data)

And I am getting error :

First currency: USD
Second currency: INR
Traceback (most recent call last):
File "c:/Users/kunal/lecture4/currency1.py", line 6, in <module>
res = requests.get("http://data.fixer.io/api/latest",params={"access_key":a8fc1a37ad16c61174a1f0395381ae41, "base": base, "symbols": other})
NameError: name 'a8fc1a37ad16c61174a1f0395381ae41' is not defined

Solution

  • Since a8fc1a37ad16c61174a1f0395381ae41 is an api key, it should be formatted as a string:

    res = requests.get("http://data.fixer.io/api/latest",params={"access_key":"a8fc1a37ad16c61174a1f0395381ae41", "base": base, "symbols": other})