newbie: good reasons for learning Python?
Mel Wilson
mwilson at the-wire.com
Sat Jun 21 12:25:45 EDT 2003
In article <o1NIa.611$hY1.47591 at news20.bellglobal.com>,
"Sean Ross" <sross at connectmail.carleton.ca> wrote:
>Better yet, go here:
>http://99-bottles-of-beer.ls-la.net/
>
>BTW, you can do something similar to the Ruby version in Python, though the
>"and/or" idiom is *strongly* discouraged for its potential pitfalls. (Let's
>go, PEP 308! heh)
>
>for i in xrange(99, 0, -1):
> print "%s bottle%s of beer on the wall" % (i, i > 1 and 's' or '')
> print "%s bottle%s of beer" % (i, i > 1 and 's' or '')
> print "take one down, pass it around"
> print "%s bottle%s of beer on the wall\n" % (i==1 and 'no' or i-1, i !=
>2 and 's' or '')
Inspiring. But after I'd improved on it, I went to the
web site and found out I'd only recreated the programs you
were writing an alternative to. Oh well.
Although
's'[:i != 1]
is a cute way to generate the plural endings.
Regards. Mel.
verse = """%(N)s bottle%(P)s of beer on the wall,
%(N)s bottle%(P)s of beer
take one down, pass it around
%(N1)s bottle%(P1)s of beer on the wall
"""
for i in xrange (99, 0, -1):
print verse % {'N':i, 'P':'s'[:i != 1]
, 'N1':i-1 or 'no', 'P1':'s'[:i-1 != 1]}
Another way is to eliminate the named substitutions, and
build the substitution tuple as
((i, 's'[0:i != 1])*2 + (i-1 or 'no', 's'[0:i-1 != 1]))
but that too was anticipated in the web site. Hmmph.
More information about the Python-list
mailing list