July 19th, 2004, 09:45 PM
How do I defeat the Index out of range error
I am working with the pyAmazon module to download Product Information from Amazon. I am having problems determining the highest item number. I keep trying higher index numbers until I get a traceback error.
What is the best way to trap the error to get the last item in the index? I know this sounds like a stupid question, but I am new to Python and am not sure about the best way to handle this. Here is how I get the error.
>>> Results = amazon.searchByKeyword('"python in a nutshell"', product_line="books", type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate=None)
Traceback (most recent call last):
File "<pyshell#38>", line 1, in -toplevel-
IndexError: list index out of range
July 19th, 2004, 10:25 PM
Why not just do a len(Results) which will let you know how many items are in your array. Remember that the array is 0-based, which means the return value of len is 1 greater than your max index value.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
July 20th, 2004, 05:25 AM
I can't believe I did not think of that!
July 20th, 2004, 11:25 AM
Depending on how you mean defeat, you could use a try-except block to catch the error before it escapes and your program burns.
Though its even better if you can stop the error before it even starts - as Scorpy explained .
…whatever using an index…
…Deal with the index error…