Newbie advice for string fromatting
Hannu Kankaanpää
hanzspam at yahoo.com.au
Wed Jul 2 15:40:16 EDT 2003
gtewalt at earthlink.net (gt) wrote in message news:<f9b7c11d.0307020642.7231cd82 at posting.google.com>...
> 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.
More information about the Python-list
mailing list