Python complaints
Charles G Waldman
cgw at fnal.gov
Thu Nov 25 06:08:29 EST 1999
Gareth McCaughan writes:
> Gordon McMillan wrote:
>
> [I said:]
> >> - if/then/else works on *statements* (strictly, suites of
> >> statements) rather than on *expressions*, so I can't
> >> say
> >> print "You scored %s point%s" % (score, if score==1 then ""
> >> else "s")
> >> or anything like that. (Or, as some people prefer to put it,
> >> Python has no ? : operator.)
> >
> >>>> def p(score):
> > ... print "You scored %d point%s" % (score, score==1 and ' '
> > or 's')
> > ...
> >>>> p(1)
> > You scored 1 point
> >>>> p(3)
> > You scored 3 points
> >>>>
>
> Yes. Unfortunately, as we all know, this doesn't work when
> it's (say) 1 and 0 you're choosing between instead of "s" and ""...
>
You can often (ab)use dictionaries to get a ?:-like effect, as in this
construction:
def p(score):
print "You scored %d point%s" % (score, {1:''}.get(score,'s'))
More information about the Python-list
mailing list