Newbie advice for string fromatting

Discussion in 'Python' started by gt, Jul 2, 2003.

  1. gt

    gt Guest

    O.k., four days into playing with python
    and I have a little problem that I would like
    to get some feedback on.
    ( as far as the best way to go about it )

    Basically, just a little Mad-Libs type script.

    Something like:

    libs = ["adverb", "noun", "verb", "tool"]
    words = {a:j, n:j, v:j, t:j}
    for x in libs:
    print "Enter a ", x, ": ",
    words[j] = raw_input()
    print "The %s %s %s with a %s." % (a, n, v, t)

    What is the most efficient way to do something like this?
    gt, Jul 2, 2003
    #1
    1. Advertising

  2. gt

    Sean Ross Guest

    I don't know whether this is efficient, but its an example of your code in a
    form that actually works:

    words = {"adverb":None, "noun":None, "verb":None, "tool":None}
    keyorder = ["adverb", "noun", "verb", "tool"] # modify dictionary in this
    order
    for word in keyorder:
    print "\nEnter a(n)", word, ": ",
    words[word] = raw_input()
    print "\nThe %(adverb)s %(noun)s %(verb)s with a %(tool)s." % words


    "gt" <> wrote in message
    > libs = ["adverb", "noun", "verb", "tool"]
    > words = {a:j, n:j, v:j, t:j}


    For the code above to work as-is, all of a, n, v, t, and j would have to be
    variables (which is not the case). In my version, I've used your libs words
    as keys to the dictionary, and I've given each dictionary item a value of
    None to start with. Also, since dictionaries do not maintain items in the
    order that they are added, and because you appear to want to ask for words
    in a particular order, I've used the list 'keyorder'. If order was
    unimportant, you could get rid of keyorder altogether and just use:

    ....
    for word in words:
    ...
    ....

    But then you get things like "Enter a(n) tool: ", "Enter a(n) noun: ",
    etc...

    [snip]
    > print "The %s %s %s with a %s." % (a, n, v, t)


    Instead of using the "string" % tuple, you can also use "string with
    keywords" % dict,
    which allows you to re-use your 'words' dictionary (no need to create
    variables a, n, v, t).

    HTH
    Sean
    Sean Ross, Jul 2, 2003
    #2
    1. Advertising

  3. "gt" <> wrote in message
    news:...
    > O.k., four days into playing with python
    > and I have a little problem that I would like
    > to get some feedback on.
    > ( as far as the best way to go about it )
    >
    > Basically, just a little Mad-Libs type script.
    >
    > Something like:
    >
    > libs = ["adverb", "noun", "verb", "tool"]
    > words = {a:j, n:j, v:j, t:j}
    > for x in libs:
    > print "Enter a ", x, ": ",
    > words[j] = raw_input()
    > print "The %s %s %s with a %s." % (a, n, v, t)
    >
    > What is the most efficient way to do something like this?


    You're on the right track.
    Using dictionaries with the % formatting operator is efficient.

    Subclassing a dictionary with a question asker is an
    approach that keeps the code simple and separates it
    from the actual madlib strings.

    So, here is version to get you started:


    >>> farmtale = """

    At %(name)s farms, we drive a %(machine)s over
    the %(vegetable)s crops and feed %(food)s to our
    %(animal)s."""

    >>> class Madlib(dict):

    def __getitem__(self, key):
    return raw_input("Enter a %s: " % key)


    >>> print farmtale % Madlib()

    Enter a name: Donald
    Enter a machine: clock
    Enter a vegetable: squash
    Enter a food: pizza
    Enter a animal: snake

    At Donald farms, we drive a clock over
    the squash crops and feed pizza to our
    snake.


    Raymond Hettinger
    Raymond Hettinger, Jul 2, 2003
    #3
  4. (gt) wrote in message news:<>...
    > O.k., four days into playing with python
    > and I have a little problem that I would like
    > to get some feedback on.
    > ( as far as the best way to go about it )
    >
    > Basically, just a little Mad-Libs type script.
    >
    > Something like:
    >
    > libs = ["adverb", "noun", "verb", "tool"]
    > words = {a:j, n:j, v:j, t:j}
    > for x in libs:
    > print "Enter a ", x, ": ",
    > words[j] = raw_input()
    > print "The %s %s %s with a %s." % (a, n, v, t)
    >
    > What is the most efficient way to do something like this?


    This is pretty compact :)

    libs = ["adverb", "noun", "verb", "tool"]
    words = [raw_input("Enter a " + x + " : ") for x in libs]
    print "The %s %s %s with a %s." % tuple(words)

    Could be even written as a perverted one-liner.

    ....Maybe a real 'for' loop for getting the words would be better
    in this case though. So that the input could be verified too.
    =?ISO-8859-1?Q?Hannu_Kankaanp=E4=E4?=, Jul 2, 2003
    #4
  5. gt

    gt Guest

    "Raymond Hettinger" <> wrote in message news:<R2EMa.9$>...
    > "gt" <> wrote in message
    > news:...
    > > O.k., four days into playing with python
    > > and I have a little problem that I would like
    > > to get some feedback on.
    > > ( as far as the best way to go about it )
    > >
    > > Basically, just a little Mad-Libs type script.
    > >
    > > Something like:
    > >
    > > libs = ["adverb", "noun", "verb", "tool"]
    > > words = {a:j, n:j, v:j, t:j}
    > > for x in libs:
    > > print "Enter a ", x, ": ",
    > > words[j] = raw_input()
    > > print "The %s %s %s with a %s." % (a, n, v, t)
    > >
    > > What is the most efficient way to do something like this?

    >
    > You're on the right track.
    > Using dictionaries with the % formatting operator is efficient.
    >
    > Subclassing a dictionary with a question asker is an
    > approach that keeps the code simple and separates it
    > from the actual madlib strings.
    >
    > So, here is version to get you started:
    >
    >
    > >>> farmtale = """

    > At %(name)s farms, we drive a %(machine)s over
    > the %(vegetable)s crops and feed %(food)s to our
    > %(animal)s."""
    >
    > >>> class Madlib(dict):

    > def __getitem__(self, key):
    > return raw_input("Enter a %s: " % key)
    >
    >
    > >>> print farmtale % Madlib()

    > Enter a name: Donald
    > Enter a machine: clock
    > Enter a vegetable: squash
    > Enter a food: pizza
    > Enter a animal: snake
    >
    > At Donald farms, we drive a clock over
    > the squash crops and feed pizza to our
    > snake.
    >
    >
    > Raymond Hettinger



    Thanks Raymond, and Sean.
    I posted at work this morning, so I didnt have Python
    available to see if my idea actually worked or not.
    It's good to know I wasn't totally off base though.
    And, I'm making an educated guess, but I suppose
    one could type up fairly large tales, let a user choose a
    category of a sort, and just import the tale modules.
    Thanks again. :)
    gt, Jul 2, 2003
    #5
    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. Paul Say

    DataList Fromatting Cell

    Paul Say, Jun 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    377
    Eliyahu Goldin
    Jun 26, 2005
  2. Asun Friere
    Replies:
    1
    Views:
    492
    Paul Boddie
    Aug 27, 2003
  3. Peter Hansen
    Replies:
    23
    Views:
    861
    Anton Vredegoor
    Sep 5, 2003
  4. sir_alex

    Fromatting an xml file

    sir_alex, Feb 2, 2006, in forum: Python
    Replies:
    2
    Views:
    286
  5. Lit
    Replies:
    4
    Views:
    351
Loading...

Share This Page