Story generator....
Terry Reedy
tjreedy at udel.edu
Mon Dec 3 14:27:14 EST 2007
"Joseph king" <king.a.joe at gmail.com> wrote in message
news:d07a228d0712030903k6139c4edvcbdf7180fcc2093 at mail.gmail.com...
|I don't know how to pose this question so i will just say what i am trying
| to do.
|
| i am not sure if this is at all possible.... i am trying to make a story
| generator.... like the ones that i did in elementry school where they
would
| supply a story with blanks and you would have to place the
nouns-verbs/you
| best friends name and it would make a funny story.
|
| but i don't know how this would be ... i might be looking at it to
simply.
| i have tried the lines with '%s' but there is no way to print that to the
| correct place that i have found with the raw_input what the person would
| create.
| i have also tried to do raw_input of where they should place the noun or
| name in the sentence but it does not print correctly.
>>> story = '''The %(actor)s %(act)s the %(victim)s'''
>>> labels = 'actor', 'act', 'victim'
>>> words = {}
>>> for blank in labels:
words[blank] = raw_input(blank+': ')
actor: giant
act: squashed
victim: python
>>> print story % words
The giant squashed the python
>>>
Q. How do I make sure the labels in the format string match the keys in the
dict?
A. Write them only once and use that to generate *both*.
>>> mstory = '''The %%(%s)s %%(%s)s the %%(%s)s'''
>>> astory = mstory % labels
>>> astory
'The %(actor)s %(act)s the %(victim)s'
>>> astory == story
True
What I would actually do is write the story with just '%' for each blank
and use the replace method to change that to '%%(%s)s'. Note that the
labels, which are also used for prompting the user, can have blanks:
>>> '%(a string)s' % {'a string': 'okay'}
'okay'
Or I might write 'story' as I originally did, and write a function to
extract the labels (checking for duplicates).
You might want to use the textwrap module for output.
Terry Jan Reedy
More information about the Python-list
mailing list