been facing an issue when coding the last part of task 1, I always end up getting a KeyError: "DEF"
. I am unsure if I missed a step or if I did something incorrectly. Thanks for the help
I attached the code and the snip of the problem encountered if somebody has any idea where the problem could be?
Main
if name == "main":
for _ in range(N):
quotes = json.loads(urllib.request.urlopen(QUERY.format(random.random())).read())
prices = {}
for quote in quotes:
stock, bid_price, ask_price, price = getDataPoint(quote)
prices[stock] = price
print ("Quoted %s at (bid:%s, ask:%s, price:%s)" % (stock, bid_price, ask_price, price))
print ("Ratio %s" % (getRatio(prices['ABC'],prices['DEF'])))[enter image description here][1]
The only keys assigned to your prices
dictionary are the stock
values.
In your final print call you try and access the value of (the price of) the key (the stock) 'DEF'
.
Your error: KeyError: "DEF".
is telling you that there was no stock called 'DEF'
, so Python doesn't no what to do!
So you need to decide how you want to fix this - probably checking before hand that the stock 'DEF'
was returned from your data scrape by seeing if 'DEF' in prices
.
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.