Newbie advice for string fromatting

G

gt

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?
 
S

Sean Ross

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 said:
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
 
R

Raymond Hettinger

gt said:
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:

At %(name)s farms, we drive a %(machine)s over
the %(vegetable)s crops and feed %(food)s to our
%(animal)s."""
def __getitem__(self, key):
return raw_input("Enter a %s: " % key)

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
 
?

=?ISO-8859-1?Q?Hannu_Kankaanp=E4=E4?=

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.
 
G

gt

Raymond Hettinger said:
gt said:
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:

At %(name)s farms, we drive a %(machine)s over
the %(vegetable)s crops and feed %(food)s to our
%(animal)s."""
def __getitem__(self, key):
return raw_input("Enter a %s: " % key)

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. :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top