python syntax for conditional is unfortunate

Asun Friere afriere at yahoo.co.uk
Wed Sep 24 21:32:52 EDT 2008


On Sep 25, 3:16 am, Pete Forman <pete.for... at westerngeco.com> wrote:
> Asun Friere <afri... at yahoo.co.uk> writes:
>
>  > A canonical use of the conditional operator is in
>  > pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else '').
>
> That fails for n == 1.  So what is best?
>

Sorry missing parentheses.  I should test before posting, even for
code written into.

> for i in range(4):
>     print '%d thing' % i + ('s' if i != 1 else '')

That is the correct version of what I meant, but your last, including
all variables for placeholders in the tuple is probably better.



More information about the Python-list mailing list