if And else Are My New Best Friends

I’ve been trying to figure out how to put my ROT13 Encoder and ROT13 Decoder into a single program since I first wrote them three weeks ago. (Really? It seems much longer than that…) Anyway, I figured out how to do it with if and else, so now ROT13 Encoder and ROT13 Decoder are no more — and ROT13er has taken both their places!

ROT13er can both decode and encode ROT13, but also has the same while magic that makes sure users enter an option when required. I also found out by accident what I should have known all along — I don’t need two different translation tables like I had for the originals because ROT13 is its own decoder and encoder (e.g., A -> O -> A), so I could remove an entire expression, even though the program now does more! …which also means that having two different programs was completely redundant in the first place 🙄 . I guess experience really is the best teacher.

Anyway, here for you to peruse is rot13er.v1-0-0.py:

ROT13er v1.0.0

ROT13er v1.0.0

#!/usr/bin/env python

# rot13er.py v1.0.0
# ROT13er
#
# Changelog:
# v1.0.0 (09/25/2010) + Initial release
#                     * Combines ROT13 Encoder & ROT13 Decoder
#
# 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 "ROT13er v1.0.0"
print "Copyright 2010 by Benjamin Braithwaite"
print
chooser = ""
while not chooser.strip():
    chooser = raw_input("Do you want to encode into (e) or decode ROT13 (d)? [Default is encode]: ")

# if user hits <d>, do decoding magic

if chooser == "d":
    print
    toberotted = ""
    while not toberotted.strip():
        toberotted = raw_input("Enter the text you wish to encode into ROT13: ")
    print
    print toberotted.translate(rot13table)
    print

# if user hits anything other than <d>, do encoding magic

else:
    print
    tobederotted = ""
    while not tobederotted.strip():
        tobederotted = raw_input("Enter the ROT13 text you wish to decode: ")
    print
    print tobederotted.translate(rot13table)
    print

# successful exit dialog

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

You can also download rot13er.v1-0-0.py from my Dropbox.

I also updated Phonebook, which now uses while loops to make sure the user enters something when asked (like pretty much all my other programs to date). I also used the same trick as in ROT13er to make the default request a phone number if the user enters an invalid option. I present to you, O Mighty Interwebs, phonebook.v1-0-1.py:

Phonebook v1.0.1

Phonebook v1.0.1

#!/usr/bin/env python

# phonebook.py v1.0.1
# Phonebook
#
# Changelog:
# v1.0.1 (09/25/2010) + Ensures user enters data
#                     + Returns phone number by default if wrong key pressed
# v1.0.0 (09/12/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/>.

# dictionary database of Alice, Bob, & Cecilia's phone/addr info

peopledb = {
	"Alice": {"phone": "(555) 241-0244", "addr": "123 Happy Street"},
	"Bob": {"phone": "(555) 241-3410", "addr": "456 Cherry Avenue"},
	"Cecilia": {"phone": "(555) 813-5801", "addr": "789 Neverending Circle"}
}

# user interface 😛

print "Phonebook v1.0.1"
print "Copyright 2010 by Benjamin Braithwaite"
print
print "Sample phonebook application"
print
name = ""
while not name.strip():
	name = raw_input("Name to look up (Alice, Bob, or Cecilia): ")
print
whatinfo = ""
while not whatinfo.strip():
	whatinfo = raw_input("Do you want %s\'s phone number (p) or address (a)? [Default is phone]: " % (name))
print

# choose output based on what info user requests, if bad request assume phone

if whatinfo == "p": key = "phone"
if whatinfo == "a": key = "addr"
else: key = "phone"

# format depending on info requested

whatformat = {"addr": "address", "phone": "phone number"}

# print output requested if available, else print error message

if name in peopledb: print "%s\'s %s is %s." % (name, whatformat[key], peopledb[name][key])
else: print name + "\'s name is not in the database."
print

# successful exit dialog

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

You can also download phonebook.v1-0-1.py from my Dropbox.

I’m still working on that Morse Code translator, but I’m not sure how to split a raw_input string into individual elements, look up each element in a dictionary, replace them, then turn all the individual elements back into a string. Perhaps there is a better/simpler way, but that’s all I can think of at the moment as a potential solution, and I don’t quite know how to translate it into working Python code. *sigh* I’ll get it eventually.

Advertisements

One thought on “if And else Are My New Best Friends

  1. Pingback: *Facepalm* (Sorry About The Meme) « 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