Mad Libs, ROT13, Python, and Me

Firstly, I apologize for the nearly week-long lack of posts on BG. The main reason for it was that I didn’t really have anything worth writing about, and I figured bothering my readers with empty words would be rather rude, and I should wait to post something worth your guys’ time. The not-main-but-still-really-important-reason for no posts is that I have no non-work Internet connectivity — otherwise you’d have been reading this yesterday. In short, I’m sorry, I hope it never happens again. Peace? ‘Kay.

Secondly, this Labor Day weekend was quite fruitful for my efforts in learning Python. I managed to crank out a few new programs based on my newly acquired knowledge of string methods. I also have begun putting copyright info into my programs, so it’s explicit where they’ve come from and what the rights are (which are fairly liberal).

Madlibs v1.0.0

Madlibs v1.0.0

The first program of the weekend was Madlibs. It’s like the famous Mad Libs games, where you fill in the blanks of a story with whatever words come to mind, making for some very nonsensical but hilarious reading. I am absolutely positive that there are way prettier ways to do this, but here goes nothing. I present madlibs.v1-0-0.py:

#!/usr/bin/env python

# madlibs.py v1.0.0
# Madlibs-like game
#
# Changelog:
# v1.0.0 (09/03/2010) - Initial release
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# imports template strings capability

from string import Template

# hello to users!

print "Welcome to Madlibs!"
print
print "Type a word or words as directed, and enjoy the silly results!"
print

# madlib template

madlib100Template = Template('I was $vbact01 down the $ncommon01 one \
$unittime01, and I $vbpast01 a $ncommon02. Not knowing what a \
$ncommon02 was, I immediately $vbpast02 and the $ncommon02 $vbpast03! \
A $noccupation01 looked my way, and I $vbpast04. Luckily, the \
$noccupation01 didn\'t notice, and it was all $adj01. $excl01!')

# create and fill dictionary of user's choices

madlib100Choices = {}

madlib100Choices['vbact01'] = raw_input("Active verb: ")
madlib100Choices['ncommon01'] = raw_input("Common noun: ")
madlib100Choices['unittime01'] = raw_input("Unit of time: ")
madlib100Choices['vbpast01'] = raw_input("Past tense verb: ")
madlib100Choices['ncommon02'] = raw_input("Common noun: ")
madlib100Choices['vbpast02'] = raw_input("Past tense verb: ")
madlib100Choices['vbpast03'] = raw_input("Past tense verb: ")
madlib100Choices['noccupation01'] = raw_input("Occupation: ")
madlib100Choices['vbpast04'] = raw_input("Past tense verb: ")
madlib100Choices['adj01'] = raw_input("Adjective: ")
madlib100Choices['excl01'] = raw_input("Exclamation: ")

# insert user's choices into madlib template & print

print madlib100Template.substitute(madlib100Choices)

raw_input ('Press <enter> to exit.')

You can download madlibs.v1-0-0.py from my Dropbox.

ROT13 Encoder v1.0.0

ROT13 Encoder v1.0.0

On my free Monday morning :mrgreen: , I sat down and buried myself in Hetland for several hours until I got the string formatting down. My head now hurts… a lot. But understanding string formatting is worth it because there’s now a brand new set of ROT13 tools on teh interwebs, starting with rot13encoder.v1-0-0.py:

#!/usr/bin/env python

# rot13encoder.py v1.0.0
# ROT13 encoder
#
# Changelog:
# v1.0.0 (09/06/2010) - Initial release
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# imports maketrans capability

from string import maketrans

# generate ROT13 translation table

rot13table = maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",\
"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm")

# user interface and encoding magics

print "ROT13 Encoder v1.0.0"
print "Copyright 2010 by Benjamin Braithwaite"
print
toberotted = raw_input("Enter the text you wish to encode into ROT13: ")
print
print toberotted.translate(rot13table)
print

raw_input ('Press <enter> to exit.')

And you can of course download rot13encoder.v1-0-0.py from my Dropbox.

ROT13 Decoder v1.0.0

ROT13 Decoder v1.0.0

Unfortunately, I don’t yet know how to do if/then statements, so the decoder is a separate program. My goal is to one day combine the two programs into a single ROT13 program which can do other things like all caps, 5-letter groups, junk letters, etc.

I give to you rot13decoder.v1-0-0.py:

#!/usr/bin/env python

# rot13decoder.py v1.0.0
# ROT13 decoder
#
# Changelog:
# v1.0.0 (09/06/2010) - Initial release
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# imports maketrans capability

from string import maketrans

# generate ROT13 translation table

derot13table = maketrans("NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm",\
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")

# user interface and decoding magics

print "ROT13 Decoder v1.0.0"
print "Copyright 2010 by Benjamin Braithwaite"
print
tobederotted = raw_input("Enter the ROT13 text you wish to decode: ")
print
print tobederotted.translate(derot13table)
print

raw_input ('Press <enter> to exit.')

Yes, you can download rot13decoder.v1-0-0.py from my Dropbox.

So this weekend was rather full of fun for me — aside from the mind-numbingly daunting Hetland chapter on string formatting — and I think I’m starting to understand Python enough to “choose my own adventure” when it comes to making educational test programs. In fact, all the programs in this post were ones I came up with on my own, from concept to execution, for which I am quite proud 😀 . The next chapter in Hetland is on dictionaries, but what I’m really excited for is the chapter afterwards: “Conditionals, Loops, and Some Other Statements”. Yes! If/then statements! Then I can do cool stuff with all this data I can manipulate (like asking whether someone wants to decode or encode ROT13 😛 ).

E-mail Quotation Reformatter v1.0.0

E-mail Quotation Reformatter v1.0.0

BONUS: I came up with a piddly program that strips those annoying “> > > > > ” that come from multiple quotations of the same e-mail, and that make it so annoying to reformat if you want to quote them cleanly. Unfortunately, my program only knows how to do one line at a time (in fact, I think all the programs I’ve written so far can only handle one line at a time — I hope I learn how to fix that sometime soon…), so it’s not particularly useful more so than doing it by hand, but there you go 😛 . For your consideration, emailquotationreformatter.v1-0-0.py

#!/usr/bin/env python

# emailquotationreformatter.py v1.0.0
# Strips initial spaces and >'s from e-mail quotations
#
# Changelog:
# v1.0.0 (09/06/2010) - Initial release
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

print "E-mail Quotation Reformatter v1.0.0"
print "Copyright 2010 by Benjamin Braithwaite"
print
qtostrip = raw_input("Enter the e-mail quotation you wish to reformat: ")
print
print qtostrip.lstrip(" >")
print

raw_input ('Press <enter> to exit.')

You can even download emailquotationreformatter.v1-0-0.py from my Dropbox, piddlingly useful though it is. Like most of my other programs, I hope to expand upon it and make it more useful someday when I learn how. Cheers!

UPDATE: Added screenshots.

Advertisements

3 thoughts on “Mad Libs, ROT13, Python, and Me

  1. Pingback: Learning About Python Dictionaries « Becoming A Glider

  2. Pingback: Learning To Use while Loops « Becoming A Glider

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