简体   繁体   中英

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

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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM