# conditional expressions

Terry Reedy tjreedy at udel.edu
Fri Sep 27 23:25:21 EDT 2002

```"Fernando Pérez" <fperez528 at yahoo.com> wrote in message
> Mike Rovner wrote:
>
> >
> > "Terry Reedy" <tjreedy at udel.edu> wrote in message
> > news:t_3l9.334924\$AR1.14880141 at bin2.nnrp.aus1.giganews.com...
> >> na=1; np=3; nl=1; ng=0
> >> print "Found: %d apple%s, %d plum%s, %d lemon%s, and %d grape%s."
%\
> >> (na, na!=1 and 's' or '', np, np!=1 and 's' or '', nl, nl!=1 and
's'
> >> or '', ng, ng!=1 and 's' or '')
> >>
> >> Found: 1 apple, 3 plums, 1 lemon, and 0 grapes.

> > 4. Try:
> > use_s=('','s')
> > print "Found: %d apple%s, %d plum%s, %d lemon%s, and %d grape%s."
%\
> > (na, use_s[na!=1], np, use_s[np!=1], nl, use_s[nl!=1], ng,
use_s[ng!=1])

Of course.

> Why not just a simple function?
>
> In [23]: plur=lambda s,n:'%s %s' % (n,s+('','s')[n!=1])
> In [27]: na=1; np=3; nl=1; ng=0
> In [28]: print "Found %s, %s, %s, %s" %
(plur('apple',na),plur('plum',np),
>    ....: plur('lemon',nl),plur('grape',ng))
> Found 1 apple, 3 plums, 1 lemon, 0 grapes

Nice.  I like both of these better than my version.  Thanks.

Terry J. Reedy

```