[Tutor] Out of range

Adam Vardy Adam Vardy <anvardy@roadrunner.nf.net>
Tue Feb 4 16:26:40 2003


Tuesday, February 4, 2003, 1:54:46 PM, you wrote:
>>
>>There is actually. In a little loop in a function, it counts up.
>>Trying to read a file. And it worked fine with a previous file. No
>>errors. But there's some problem now.

>> Show your code. There is no way we can help you unless
>> we can see what you are really doing.

Here is the described function.  The 1st IF statement was added to try
to repair things after the last message.

I had to add a variable, index, because my FOR statement does not keep
track of the numeric ordinal index of the FOR loop. If it counted 7
times, then it would be counting 1,2,3,4,5,6,7.  Which I think it
must do internally anyway, so realistically, we shouldn't be having to
add an extra variable.  And make the function longer, and something to
complain about.

This function tries to adapt the prefix somewhere so the whole thing
will sort properly.

---------------------------------------------

def fixit(text):
    index=0
    if text[0].isdigit()==0:
        return text
    for i in text:
        if i.isalpha()==1:
            break
        index=index+1

    out=text[index]
    num=text[:index]
    rest=text[index:]
##    print"out:"+out
##    print"num:"+num
##    print"rest:"+rest
##    
    return out+num+rest

-----------------------------------------------

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

-- 
Adam Vardy