//
you're reading...
Uncategorized

Madlibs in Python

"""
madlib2.py
Interactive display of a mad lib, which is provided as a Python format string,
with all the cues being dictionary formats, in the form %(cue)s.

"""

def getCues(formatString):
    keyList = list()
    end = 0
    repetitions = formatString.count('%(')
    for i in range(repetitions):
        start = formatString.find('%(', end) + 2
        end = formatString.find(')', start)
        key = formatString[start : end]
        keyList.append(key) # may add duplicates
    return set(keyList) # removes duplicates: no duplicates in a set


def getUserPicks(cues):
    userPicks = dict()
    for cue in cues:
        prompt = "Enter a specific example for %s: " % cue     
        userPicks[cue] = raw_input(prompt)
    return userPicks   

def tellStory(story):
    cues = getCues(story)
    print cues
    userPicks = getUserPicks(cues)
    print userPicks
    print story % userPicks    

def main():
    originalStory = '''
    
Once upon a time, deep in an ancient jungle, there lived a %(animal)s. 
This %(animal)s liked to eat %(food)s, but the jungle had
very little %(food)s to offer.  

One day, an explorer found the %(animal)s and discovered
it liked %(food)s.  The explorer took the
%(animal)s back to %(city)s, where it could
eat as much %(food)s as it wanted.  

However, the %(animal)s became %(negative adjective)s, so the
explorer brought it back to the jungle, and left it there 
with a large supply of %(food)s. So the %(animal)s was not 
%(negative adjective)s any more.

The End
'''
    tellStory(originalStory)

main()
Advertisements

Discussion

2 thoughts on “Madlibs in Python

  1. amazing 5 stars

    Like

    Posted by ethan | May 6, 2015, 4:32 pm
  2. def getCues(formatString):
    keyList = list()
    end = 0
    repetitions = formatString.count(‘%(‘)
    for i in range(repetitions):
    start = formatString.find(‘%(‘, end) + 2
    end = formatString.find(‘)’, start)
    key = formatString[start : end]
    keyList.append(key) # may add duplicates
    return set(keyList) # removes duplicates: no duplicates in a set

    def getUserPicks(cues):
    userPicks = dict()
    for cue in cues:
    prompt = “Enter a specific example for %s: ” % cue
    userPicks[cue] = raw_input(prompt)
    return userPicks

    def tellStory(story):
    cues = getCues(story)
    print cues
    userPicks = getUserPicks(cues)
    print userPicks
    print story % userPicks

    def main():
    originalStory = ”’

    one time a boy started to %(verb)s he did %(verb)s for a long time
    then he went to %(place)s by %(way_of_traviling)s. it soon got
    boring and he went to %(place)s and there he ate %(food)s,
    and some more %(food)s

    when he was full he tried to %(different_verb)s he failed %(exprassion_of_size)s
    then he %(different_way_of_traviling)s all the way home and watched %(wierd_thing)s channel on ESPN.

    the end
    ”’
    tellStory(originalStory)

    main()

    Like

    Posted by ethan | May 7, 2015, 4:21 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: