Python equivalent to a C trick

Josef Meile jmeile at hotmail.com
Wed Aug 11 04:02:14 EDT 2004


Paul McGuire wrote:
> No offense taken! :)
> 
> The OP was looking for a general solution to <condition> ? <if-true-action>
> : <else-action>, then gave us the "you have n egg(s)" example.
So, I guess it is like the "foo" keyword.

> In the general case, the number for comparison isn't always an integer
> quantity such as eggs.  It could be "dollar(s)"/"euro(s)"/"Swiss
> franc(s)"/"yen" (hmm, I guess "yen" isn't a problem...), or "ton(s) of
> salami", or "pound(s) of cement", or "degree(s) Celsius", or...  Anyway,
> even though eggs are usually counted from 1 to n in integer steps, other
> quantities can easily be negative and/or continuous.  Still the singular -
> in English, anyway - is usually used *only* when the quantity is 1.
> Fractional and zero amounts, even though less than 1, still most naturally
> use the plural form.
> 
> You have 0.5 dollars
> You have gained 1 pound
> You increased temperature by 0 degrees
> You have -2 dollars (that is, you owe 2 dollars)
Good point ;-)

> My point (which I guess didn't come across too well) was that this is a
> typical coding and testing error, in which only positive integer values > 0
> are assumed, because we often mentally equate "plural" with "more than 1".
> But whether you are working in an integer, real, positive-only, or all
> numbers context, testing with n != 1 should determine whether singular noun
> should be used.
I like your solution and I think it even considers when n=0, while the 
other example fails and write "You have 0 egg"



More information about the Python-list mailing list