String manipulation in python..NEED HELP!!!!
vlastimil.brom at gmail.com
Tue Dec 11 00:25:58 CET 2012
2012/12/10 <qbailey at ihets.org>:
> I need help with a program i am doing. it is a cryptography program. i am given a regular alphabet and a key. i need to use the user input and use the regular alphabet and use the corresponding letter in the key and that becomes the new letter. i have the basic code but need help with how to mainpulate the string to do the encryption/decryption. please help
> here is my code so far:
> """ 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
> 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.
if I understand correctly, for the data shown in the code, you may
probably use the translate method of the string (and the corresponding
>>> import string
>>> "ABCDEF...VWXYZ".translate(string.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "XPMGTDHLYONZBWEARKJUFSCIQV"))
>>> "ABCDEF...VWXYZ".translate("".maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "XPMGTDHLYONZBWEARKJUFSCIQV"))
More information about the Python-list