Rewrite of the Letterback encrytion script.
Rewritten to make it more modular, basically works the same way as before.
import sys
alphabet = 'abcdefghijklmnopqrstuvwxyz'
balphabet = 'zyxwvutsrqponmlkjihgfedcba'
class LetterBack:
'''Class for Encrypting a string of letters'''
def __init__(self, text='mnm'):
self.text = text
def encrypt(self, aSentance):
'''Encrypts a given string by replacing each letter
with the letter before it in the alphabet.'''
output = ''
for element in aSentance:
for i in range(26):
if element == alphabet[i]:
output += alphabet[i-1]
print output
def decrypt(self, aSentance):
'''Decrypts a given string encoded by the encrypt
function.'''
output = ''
for element in aSentance:
#print element + ' this is 1'
for i in range(26):
if element == balphabet[i]:
output += balphabet[i-1]
print output
def main():
lb = LetterBack()
if sys.argv[1] == '-e':
lb.encrypt(sys.argv[2])
elif sys.argv[1] == '-d':
lb.decrypt(sys.argv[2])
if __name__ == '__main__':
main()
use:
python letterback.py -e 'alvin'
returns: zkuhm
python letterback.py -d 'zkuhm'
returns: alvin
or in the python interpreter:
>>> from letterback import LetterBack
>>> lb = LetterBack()
>>>lb.encrypt('alvin')
zkuhm
>>> lb.decrypt('zkuhm')
alvin
enjoy :)
