Re: newbie: good reasons for learning Python?

Discussion in 'Python' started by Sean Ross, Jun 24, 2003.

  1. Sean Ross

    Sean Ross Guest

    "Mel Wilson" <> wrote in message
    news:JcI9+ks/...
    > 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.


    Actually, I wasn't trying to make an alternative to that code, I was just
    trying to write something similar to the Ruby version I showed.
    But, while we're here:

    # version of Fredrik Lundh's code at
    # http://99-bottles-of-beer.ls-la.net/p.html#Python
    # using dict.get() rather than try/except
    verse = """
    %(b1)s on the wall, %(b1)s,
    take one down, pass it around,
    %(b0)s on the wall.
    """

    def bottle(n):
    return {0: "no more bottles",
    1: "1 bottle"}.get(n, "%d bottles"%n) + " of beer"

    for i in range(99, 0, -1):
    b1, b0 = bottle(i), bottle(i-1)
    print verse % locals()
    Sean Ross, Jun 24, 2003
    #1
    1. Advertising

  2. Sean Ross

    Max M Guest

    Sean Ross wrote:

    > Actually, I wasn't trying to make an alternative to that code, I was just
    > trying to write something similar to the Ruby version I showed.
    > But, while we're here:
    >
    > # version of Fredrik Lundh's code at
    > # http://99-bottles-of-beer.ls-la.net/p.html#Python
    > # using dict.get() rather than try/except
    > verse = """
    > %(b1)s on the wall, %(b1)s,
    > take one down, pass it around,
    > %(b0)s on the wall.
    > """
    >
    > def bottle(n):
    > return {0: "no more bottles",
    > 1: "1 bottle"}.get(n, "%d bottles"%n) + " of beer"
    >
    > for i in range(99, 0, -1):
    > b1, b0 = bottle(i), bottle(i-1)
    > print verse % locals()



    Actually I find both that and frederiks version of the code overly complex.

    This is a bit longer but should be more "pythonic" and far easier to
    understand:

    verse = """
    %(b1)s on the wall, %(b1)s,
    take one down, pass it around,
    %(b0)s on the wall.
    """

    for i in range(99, 0, -1):
    bot_1 = '1 bottle of beer'
    bot_n = '%s bottles of beer'
    if i >= 2:
    b1 = bot_n % i
    b0 = bot_n % (i-1)
    if i == 2:
    b0 = bot_1
    if i == 1:
    b1 = bot_1
    b0 = bot_n % 'no more'
    print verse % locals()


    regards Max M
    Max M, Jun 25, 2003
    #2
    1. Advertising

  3. Max M wrote:

    > Actually I find both that and frederiks version of the code overly
    > complex.


    many years ago, I stumbled upon the 99 bottle page, and
    found a C-written-in-Python-syntax example, so I sent the
    page maintainer a nice new Pythonic example, without all
    those extra characters...

    unfortunately, I couldn't keep myself from adding a much
    more obscure example as a postscript.

    guess the "pythonic edition" look too bland, or something.

    > This is a bit longer but should be more "pythonic" and far easier to
    > understand:


    well, you're still using the locals() hack.

    </F>
    Fredrik Lundh, Jun 25, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?V2ViTWF0cml4?=

    Good Reasons to port asp.NET VB.NET app to C#

    =?Utf-8?B?V2ViTWF0cml4?=, Mar 31, 2005, in forum: ASP .Net
    Replies:
    19
    Views:
    1,084
    Kevin Spencer
    Apr 1, 2005
  2. prashna
    Replies:
    8
    Views:
    658
    jeffc
    Feb 6, 2004
  3. Jiwon Seo
    Replies:
    0
    Views:
    784
    Jiwon Seo
    Jun 25, 2003
  4. Hal Vaughan
    Replies:
    7
    Views:
    472
  5. Preston Crawford

    Reasons to consider learning Ruby?

    Preston Crawford, Jan 21, 2005, in forum: Ruby
    Replies:
    16
    Views:
    182
    Phil Tomson
    Jan 23, 2005
Loading...

Share This Page