[Tutor] Strange "snippets" in Learning Python, 2nd ed.

Kent Johnson kent37 at tds.net
Sun Aug 21 03:04:05 CEST 2005



Dick Moores wrote:
> I have the first printing. The snippets are on pp. 159 and 160, and are 
> there to illustrate the loop "else" clause.
> 
> found = 0
> while x and not found:
>      if match(x[0]):              # value at front?
>          print 'Ni'
>          found = 1
>      else:
>          x = x[1:]                # slice off front and repeat
> if not found:
>      print 'not found'
> 
> 
> while x:                         # exit when x empty
>      if match(x[0]):
>          print 'Ni'
>          break                    # exit, go around else
>      x = x[1:]
> else:
>      print 'Not found'            # only here is exhausted x

This is still a pretty strange way to iterate unless the truncated list x is a desirable side effect...it could be 
for y in x:
  if match(y):
    print 'Ni'
    break
else:
  print 'Not found'

Kent
> 
> "match()" seems to come out of the blue, and also "Ni". Or have I 
> misunderstood something?
> 
> Thanks,
> 
> Dick Moores
> rdm at rcblue.com
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list