[Tutor] Out of range

Magnus Lycka magnus@thinkware.se
Tue Feb 4 16:39:03 2003


At 17:55 2003-02-04 -03-30, Adam Vardy wrote:
>And the error I get at the moment, after adding the first IF
>statement.  Happens right away:
>
>     if text[0].isdigit()==0:
>IndexError: string index out of range

You are trying to access a part of the string that doesn't
exist. In this case the first character. It will work if you
don't call it with an empty string.

If you are certain that your function is always called
with a string, you can do this:

if (not text) or (not text[0].isdigit()):
     #handle empty string or non-numeric
     return text
...


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se