[Tutor] Splitting a string

Christian Witts cwitts at compuscan.co.za
Tue Feb 8 14:47:34 CET 2011


On 08/02/2011 15:38, David Hutto wrote:
> On Tue, Feb 8, 2011 at 8:36 AM, Christian Witts<cwitts at compuscan.co.za>  wrote:
>    
>> On 08/02/2011 15:04, tee chwee liong wrote:
>>      
>>> hi all,
>>>
>>> thanks for the advice. i modified my code to be:
>>>
>>> c=('01101')
>>> i=-1
>>> try:
>>>     while 1:
>>>         i=c.index('0',i+1)
>>>         print "Lane fail",i
>>> except ValueError:
>>>     print "All Lanes PASS"
>>>     pass
>>>
>>> when i run, the result is:
>>>
>>>        
>>>>>>              
>>> Lane fail 0
>>> Lane fail 3
>>> All Lanes PASS
>>>
>>> Question: How do i prevent the code from executing the except ValueError
>>> part. it seems to be checking bit by bit and when is see 1 it will execute
>>> the except part.
>>> i just want the results to be:
>>>        
>>>>>>              
>>> Lane fail 0
>>> Lane fail 3
>>>
>>>
>>> thanks
>>> tcl76
>>>
>>>        
>> `while i<  len(c)` instead of `while 1`
>>      
> You remind me of me...like it was yesterday. Explanations with no
> thought. Unimpressive isn't it?
>    

While the index is smaller than the length of the string do your 
processing.  It reads like that.  But thank you for your commentary.

-- 
Kind Regards,
Christian Witts




More information about the Tutor mailing list