On Tue, Feb 8, 2011 at 2:47 PM, Christian Witts <cwi...@compuscan.co.za> >>> `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. >
The goal of this list is not to criticize, David, but to help. Why not provide some explanations yourself? EIther way, even with this correction a ValueError is still raised at least *once* if the string doesn't end with a 0, so it won't actually help a bit. A quick fix is to set some variable if you find a 0, then check on that variable after the loop (See Peter's example). Now, find is nice if you care about finding one instance, but if you need to find *all* instances you'll have to check the entire string anyway, and find becomes a rather cumbersome tool. Interestingly, enumerate was suggested all the way at the beginning of the thread, and I think it's the best answer: zeroes = ["lane fail {0}".format(i) for i, val in enumerate(c) if val == '0'] print '\n'.join(zeroes) if zeroes else "All Lanes pass" Ok, That looks a little advanced, but strip away all that syntactic sugar and you arrive at basically this: zeroes = [] for index, value in enumerate(c): if value == '0': zeroes.append(index) if zeroes: for x in zeroes: print "lane fail", x else: print "all lanes pass" Since we have to scan the whole string anyway, this is equally efficient as using find (theoretically!!! if you care about performance, *measure*). Hugo _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor