[Tutor] Splitting a string

tee chwee liong tcl76 at hotmail.com
Wed Feb 9 12:44:08 CET 2011


> No, it doesn't work. You haven't sufficiently tested it. It tells lies:
> 
> 
> >>> s='11100101'
> >>> found = False
> >>> for i,c in enumerate(s):
> ... if c == '0':
> ... print 'Lane fail',i
> ... found = True
> ... if not found: print 'All lanes PASS'
> ...
> All lanes PASS
> All lanes PASS
> All lanes PASS
> Lane fail 3
> Lane fail 4
> Lane fail 6
> 
> All lanes did NOT pass.
> 

thanks for catching the bug. the code should be:
####
s='11100101'
found = False
for i,c in enumerate(reversed(s)):
    if c == '0':
        print 'Lane fail',i
        found = True
        if not found: print 'All lanes PASS'
###
 
the if not found needs to be indented. 
 
result:
>>> 
Lane fail 1
Lane fail 3
Lane fail 4
>>>  		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110209/85acac0a/attachment-0001.html>


More information about the Tutor mailing list