Python Forum
Help with Exception to error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Exception to error
#1
Hi,

I am new to Python and trying to help a friend.

I need to use a try exception to fix the error here below.

Task exception was never retrieved
future: <Task finished coro=<notification_loop() done, defined at chesterfieldbot.py:167> exception=KeyError('iv_atk',)>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
File "chesterfieldbot.py", line 201, in notification_loop
hundredsarray = getNewHundreds(pokearray)
File "chesterfieldbot.py", line 139, in getNewHundreds
if (getIVPercent(newArray[i]) != 100.0):
File "chesterfieldbot.py", line 20, in getIVPercent
if (poke['iv_atk'] is None or poke['iv_def'] is None or poke['iv_sta'] is None):
KeyError: 'iv_atk'

within the below code. Any help will be appreciated as I'm not too sure how I should be implementing this. Please let me know if more details are needed as I am new here and a noob.
code -> https://python-forum.io/Thread-Help-with...1#pid42111
Reply
#2
please delete thread
Reply
#3
We do not normally delete threads/posts. If you have a specific reason, you can post it here Private Inquiries, this is viewable only by you and the admins/mods.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
posted in there, please read and take this thread down or remove the code...!!!
Reply
#5
Hi,

I am new to Python and trying to help a friend.

I need to use a try exception to fix the error here below.

Task exception was never retrieved
future: <Task finished coro=<notification_loop() done, defined at chesterfieldbot.py:167> exception=KeyError('iv_atk',)>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
File "chesterfieldbot.py", line 201, in notification_loop
hundredsarray = getNewHundreds(pokearray)
File "chesterfieldbot.py", line 139, in getNewHundreds
if (getIVPercent(newArray[i]) != 100.0):
File "chesterfieldbot.py", line 20, in getIVPercent
if (poke['iv_atk'] is None or poke['iv_def'] is None or poke['iv_sta'] is None):
KeyError: 'iv_atk'

I have listed the code sections below by the lines in which are mentioned in the above error. Any help will be appreciated as I'm not too sure how I should be implementing this. Please let me know if more details are needed as I am new here and a noob.

I just need to try and add a try exception or some exception to these to keep them running and alive rather than throwing an error.

Line 167
#This runs the loop to post notifications about rare pokemon.
async def notification_loop():
    while not bot.is_closed():
        await bot.wait_until_ready()
        global desiredPokemon, savedRares, savedHundreds
        payload = { 
            'api-key': '*API KEY HERE*',
            'area': 'chesterfield',
            'type': 'pokemon',
            'fields': 'id,pokemon_id,lat,lon,iv_atk,iv_def,iv_sta',
        }
Line 201
 if (len(pokearray) == 0 or seriousError):
            raresarray = []
            hundredsarray = []
        else:
            raresarray = getNewRares(pokearray)
            hundredsarray = getNewHundreds(pokearray)
Line 139
or i in range(0, len(newArray)):
            if (getIVPercent(newArray[i]) != 100.0):
                indexToRemove = indexToRemove + [i]
    indexToRemove = sorted(indexToRemove, reverse=True)
    for index in indexToRemove:
        newArray.pop(index)
Line 20
def getIVPercent(poke):
    if (poke['iv_atk'] is None or poke['iv_def'] is None or poke['iv_sta'] is None):
        return 0
    else:
        return ((float(poke['iv_atk'])+float(poke['iv_def'])+float(poke['iv_sta']))/45.0)*100.0

Posted above in more detail :)
Reply
#6
You only need to change the last block of code you provided. Wrap the four lines within the function in a try, and then catch the KeyError.

I suspect that's not really what you want though. For one thing, you can replace code like
poke['iv_atk']
with
poke.get('iv_atk', None)
That aside, I find it weird that a Pokemon would not have those IVs. You probably should prevent that input being provided to that function, rather than making the function tolerant of bad input.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to write exception error into logger mg24 3 982 Nov-15-2022, 04:20 PM
Last Post: insharazzak
  pywin32 Illustrator Throwing Exception Error matthewsjc1 7 5,576 Aug-27-2021, 02:43 AM
Last Post: Larz60+
  SystemError: error return without exception set!!! faryad13 3 3,672 Oct-23-2020, 02:32 PM
Last Post: ATARI_LIVE
  Exception: Returned Type Mismatch Error devansing 1 5,158 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  i am getting error while writing code for exception logging rkgupta51179 1 1,851 Nov-03-2019, 05:12 AM
Last Post: buran
  During handling of the above exception, another exception occurred Skaperen 7 26,888 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  Can't find error in code but Python throws exception Sandwich_masterX 3 2,940 Oct-09-2018, 01:38 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020