String manipulation in python..NEED HELP!!!!

John Gordon gordon at panix.com
Mon Dec 10 17:59:05 EST 2012


In <d6779e35-32b8-417a-abf9-72454573bf56 at googlegroups.com> qbailey at ihets.org writes:

> """ crypto.py
>     Implements a simple substitution cypher
> """

> alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
> key =   "XPMGTDHLYONZBWEARKJUFSCIQV"

> def main():
>   keepGoing = True
>   while keepGoing:
>     response = menu()
>     if response == "1":
>       plain = raw_input("text to be encoded: ")
>       print encode(plain)
>     elif response == "2":
>       coded = raw_input("code to be decyphered: ")
>       print decode(coded)
>     elif response == "0":
>       print "Thanks for doing secret spy stuff with me."
>       keepGoing = False
>     else:
>       print "I don't know what you want to do..."

> i really need help on how to encrypt it im not sure how to go about doing
> that please help.

def encode(plain):
   '''Return a substituted version of the plain text.'''

   encoded = ''

   for ch in plain:
      encoded += key[alpha.index(ch)]

   return encoded

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list