python - Run a block of code each time a condition is met -
first off, sorry wall of text. try explain issue as can.
hello, title may think refer simple if
statement , may true. i've done questions today project , want else. i'm doing bot using league of legends api. bot print stats current game of given player. unfamiliar game, there ranks. rank 1 of stats bot prints chat working on; stats ranked games, unlike normal games, ranked games ones count these stats. anyway; i'm getting data json , each time player unranked (not ranked player) 404 request , want handle 404 print else. right now, important part of code:
ids_seen = set() y in range(0, 10): num += 1 = r_match['participants'][num] e_name = i['summonername'] e_id = i['summonerid'] team_id = i['teamid'] champ = i['championid'] r_team = requests.get("https://lan.api.pvp.net/api/lol/lan/v2.5/league/by-summoner/{}/" "entry?api_key=".format(e_id)).json() champ_r = requests.get("https://global.api.pvp.net/api/lol/static-data/lan/v1.2/champion?" "api_key=").json() x = r_team["{}".format(e_id)][0] e_tier = x['tier'] e_div = x['entries'][0]['division'] key, value in champ_r['data'].items(): c_name = value['name'] c_id = value['id'] chat_say = """ {} - {} {} - playing `#{}`""".format(e_name, e_tier, e_div, c_name) if champ == c_id: # if r_team['status']['status_code'] == 404: # unranked_term = (e_name + " - " + "unranked") # unranked_say = """ # {} - playing `#{}`""".format(unranked_term, c_name) # yield bot.send_message(message.channel, unranked_say) # else: if team_id == 100: if not team_id in ids_seen: yield bot.send_message(message.channel, "```---blue team---```") yield bot.send_message(message.channel, chat_say) elif team_id == 200: # if r_team['status']['status_code'] == 404: # unranked_term = (e_name + " - " + "unranked") # unranked_say = """ # {} - playing `#{}`""".format(unranked_term, c_name) # yield bot.send_message(message.channel, unranked_say) if not team_id in ids_seen: yield bot.send_message(message.channel, "```--- red team ---```") yield bot.send_message(message.channel, chat_say) ids_seen.add(team_id) yield asyncio.sleep(1)
when run code output this:
player1 - rank - champion being played player2 - rank - champion being played player3 - rank - champion being played...
... , on; while happening if finds unranked player mentioned before throws keyerror
because, well, can't find ranked data on json. so; commented out lines ones want implement.
this:
# if r_team['status']['status_code'] == 404: # unranked_term = (e_name + " - " + "unranked") # unranked_say = """ # {} - playing `#{}`""".format(unranked_term, c_name) # yield bot.send_message(message.channel, unranked_say)
i tried placing , getting keyerror
again; if place right here:
if team_id == 100: # <- right here if i'm not wrong. if not team_id in ids_seen: yield bot.send_message(message.channel, "```---blue team---```") yield bot.send_message(message.channel, chat_say)
i players , champions being played ranks of each 1 displayed unranked; unranked. question is, how can implement code? i'm not sure if i'm in right path on doing this.
i output like:
player1 - rank - champion # if has ranked information player2 - rank - champion # same player3 - unranked - champion # if player unranked
everything can run bot because every time there unranked error , can't avoid (except looking match without unranked player)
thanks help! :)
you can try adding try except
block @ exact place getting keyerror
, try value @ key, if not exist, catch exception
not exist , can handle exception
wish (in case, unranked player)
for example - if json or dict
jsonobj = { "players":[ { "rank":"500", "name":"iamranked" }, { "name":"iamnotranked" } ] }
then, if surround try except
block this:
# examples try: print(jsonobj['players'][0]['rank']) #surround assignement try catch block print("i have rank") # print rank exists except keyerror: print("i not have rank") try: print(jsonobj['players'][1]['rank']) print("i have rank") except keyerror: print("i not have rank") # print rank not exist
hope helps!
Comments
Post a Comment