[Tutor] For - if - else loop; print selective output

Alexandre BM salexandre.bm at gmail.com
Wed Oct 24 18:51:41 CEST 2012


You can break the loop when you get a positive matches, and add a 
variable where you can count the number of positive matches, if it's 
equal to zero print 'not found'

On 24/10/2012 17:43, Saad Javed wrote:
> Let me modify this example:
>
> a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', 
> '21', 'cookies']]
> for i in a:
> *b = i[0]*
>     if (i[1] == '25' or i[1] == '26'):
>         print *b*
>     else:
>         print 'Not found'
>
> This will output:
> *jimmy*
> *Not found*
> *Not found*
> *
> *
> How do I print positive matches (*jimmy*) only and not (*Not found*). 
> I also want *Not found *to print only /once /if there are no positive 
> matches.
>
> On Wednesday, October 24, 2012, Saad Javed wrote:
>
>     Hi,
>
>     a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'],
>     ['harry', '21', 'cookies']]
>     for i in a:
>         if (i[1] == '25' or i[1] == '26'):
>             print 'yes'
>     else:
>         print 'Not found'
>
>     This prints:
>     yes
>     not found
>
>     I want it to print "yes" for each positive match but nothing for a
>     negative match. However if all matches are negative, I want it to
>     print "Not found" once (That bit the code already does). I do I
>     get it to print "yes" only in a mix result situation?
>
>     Saad
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121024/5a6a28e3/attachment.html>


More information about the Tutor mailing list