Python equivalent to a C trick

Paul McGuire ptmcg at austin.rr._bogus_.com
Tue Aug 10 14:54:46 EDT 2004


"Sam Holden" <sholden at flexal.cs.usyd.edu.au> wrote in message
news:slrnchgsml.8e4.sholden at flexal.cs.usyd.edu.au...
> On 9 Aug 2004 23:55:08 -0700, Dan <danmoskowitz at gmail.com> wrote:
> > Is there a python equivalent of this trick in C?
> >
> > Logic_Test ? True_Result : False_Result
> >
> > Example:
> > printf( "you have %i %s", num_eggs, num_eggs > 1 ? "eggs" : "egg" );
>
> print "you have %i %s" % ( num_eggs, ("egg","eggs")[num_eggs>1] )

I would choose "!=" instead of ">" as the comparison operator.  I think the
accepted vernacular is:

you have -2 eggs
you have -1 eggs
you have 0 eggs
you have 1 egg
you have 2 eggs
you have 3 eggs
you have 0.5 eggs
...

-- Paul





More information about the Python-list mailing list