compound statement from C "<test>?<true-val>:<false-val>"

Holger ishoej at gmail.com
Sun Feb 11 17:16:49 EST 2007


Thanks all for good input.
It seems like there's no the-python-way for this one.

Currently I'm forced to use cygwin - and python in cygwin is still not
2.5 so I can't use the new inline if-else ternary operator.

> >> if n == 1:
> >>     print "I saw a car"
> >> else:
> >>     print "I saw %d cars" % n

Personally I don't like the if-else approach because of the don't-
repeat-yourself philosophy

> D'accord. Did I mention that, as a "for fun" approach, "s" * (n != 1) is
> quite clever :-)
>
> Peter

I like this one :-)

> print "I saw %d car%s\n" % (n, ("", "s")[n != 1])

And this one.

/Holger




More information about the Python-list mailing list