H
H
hard0g12020-07-28 23:18:31
Python
hard0g1, 2020-07-28 23:18:31

How to fix errors in python code?

This code should display items from the steam inventory and their cost
. I don't understand python code I need help fixing errors in the code


import time

import requests

wear_list = ['Factory New', 'Minimal Wear', 'Field-Tested', 'Well-Worn', 'Battle-Scarred']
INVENTORY_URL = ' http://steamcommunity.com/profiles/{} /inventory/js... '
MARKET_URL = ' steamcommunity.com/market/priceoverview '

def get_inventory(steamid):
r = requests.get(INVENTORY_URL.format(steamid))
return r.json()['rgDescriptions']

def extract_information(descriptions):
for _, item in descriptions.items():
tags = {i['category']: i for i in item['tags']}
name = tags['market_name']
yield {
'name': name[name.find('|')+1:name.find('(')].strip(),
'market_name': name,
'market_hash_name': item['market_hash_name'],
'wear ': tags['Exterior']['name'],
'gun': tags['Weapon']['name'],
'stattrak': 'StatTrak' in tags['Quality']['name'],
'marketable': item['marketable'],
}

def get_prices(items):
for item in items:
if not item['marketable']:
continue
if item['wear'] not in wear_list:
continue

r = requests.get(
MARKET_URL,
params={
'appid': '730',
'currency': '2',
'market_hash_name': item['market_hash_name']
}
)
json_data = r.json()
try:
price = json_data['lowest_price']
except KeyError :
price = json_data['median_price']

item['price'] = price[-4:]
time.sleep(5)
yield item

if __name__ == '__main__':
inventory = get_inventory('76561198996231585')
item_information = extract_information(inventory)
items = get_prices(item_information)
for item in items:
print('{name}: {price}'.format(**item))



Mistake


Traceback (most recent call last):
File "steam_price_item.py", line 59, in
for item in items:
File "steam_price_item.py", line 31, in get_prices
for item in items:
File "steam_price_item.py", line 19 , in extract_information
market_name = tags['name'],
KeyError: 'name'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-07-28
@hard0g1

The error is obvious: there is no name key in the tags dictionary . But there is a market_name . Try replacing one with the other.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question