[Edu-sig] a non-rhetorical question

kirby urner kirby.urner at gmail.com
Fri Jul 6 21:36:30 CEST 2007


Sorry, that post got away from me when I started hitting
tab keys 'n stuff.  Blaming jet lag.  Trying again...

Saved module (sillystory.py):

======
import string

def test():

    thestory = string.Template(
"""
There once was a certain $persons_name from
$city who had a pet $animal. One day, the $animal
ate all the food in the house and $persons_name
was very <i>angry</i>.
""")  # sneaking in a little HTML (looking ahead)

    print thestory  # just an object in memory

    # let's fill a dictionary!
    thedict = {"persons_name" : "",
               "animal" : "",
               "city" : "" }

    for thekey in thedict:
        answer = raw_input("Please enter " + thekey + "> ")
        thedict[thekey] = answer

    print thestory.substitute(thedict)

======

>>> import sillystory
>>> sillystory.test()
<string.Template object at 0xb6e7faac>
Please enter persons_name> Kirby
Please enter city> Vilnius
Please enter animal> Snake

There once was a certain Kirby from
Vilnius who had a pet Snake. One day, the Snake
ate all the food in the house and Kirby
was very <i>angry</i>.

So then they get to experiment with this framework,
changing the story and the matching dictionary keys,
reloading, debugging, and, we hope, getting some
positive results.

Many kids are familiar with the Mad Libs genre (anyway
it's easy to explain).  A teacher might have 'em focus
on the dictionary, probably the most important data
structure.

More on the Mad Lib approach in my EuroPython slides:
http://www.4dsolutions.net/presentations/connectingthedots.pdf

Kirby


More information about the Edu-sig mailing list