[Edu-sig] a non-rhetorical question

John Zelle john.zelle at wartburg.edu
Fri Jul 6 21:48:22 CEST 2007


I agree with Kirby that madlibs are a fun example. 

On Friday 06 July 2007 2:12 pm, kirby urner wrote:
....
> As I mention in my slides for EuroPython (looking forward to
> meeting Laura again tomorrow), Madlibs proved appealing
> to most students, little stories where they have to prompt
> themselves (since raw_input seems to be a theme here,
> though interacting with the shell and having no main loop
> at all is just as possible, I'd say better at first...)
>
> Possible to use %s substitution but string module has these
> nifty Template as in:
>
> sillystory = 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
> angry.
> """
>

One note (I meant to post this in the discussion of string.Template before): 
You can do this same thing just as simply w/o using the string library. Plain 
old string formatting works with dictionaries as well. You can just do this:

sillystory = """
There once was a certain %(persons_name)s from %(city)s
who had a pet %(animal)s.  One day, the %(animal)s ate all
the food in the house and %(persons_name)s was very
angry.
 """

fillins = dict(persons_name="Jack", city="Timbuktu", animal="lemur")

print sillystory % fillins

I also like the string library for certain pedagogical reasons, but I don't 
think the Templates are compelling when you can do essentially the same thing 
with string formatting. Is the $ notation that much of a "win?"

--John

-- 
John M. Zelle, Ph.D.             Wartburg College
Professor of Computer Science    Waverly, IA     
john.zelle at wartburg.edu          (319) 352-8360  


More information about the Edu-sig mailing list