I am new to Python and I'm trying to create a Caesar Cipher. So far, I've been using my own knowledge of Python and some online resources and I have managed to come up with the code below.
I wanted the program to be able to take both number and text values and shift them by 13 and 5. Initially, when testing for text values the program seemed to work but after adding the code to include numbers it stopped working.
Now, it does not output anything.
I have included the code and I am using IDLE 3.6 key = 'abcdefghijklmnopqrstuvwxyz'
def encrypt(n, plaintext):
"""Encrypt the string and return the ciphertext"""
result = ''
for l in plaintext.lower():
try:
i = (key.index(l) + n) % 26
result += key[i]
except ValueError:
result += l
return result.lower()
def decrypt(n, ciphertext):
"""Decrypt the string and return the plaintext"""
result = ''
for l in ciphertext:
try:
i = (key.index(l) - n) % 26
result += key[i]
except ValueError:
result += l
return result
def show_result(plaintext, n):
"""Generate a resulting cipher with elements shown"""
encrypted = encrypt(n, plaintext)
decrypted = decrypt(n, encrypted)
print 'Rotation: %s' % n
print 'Plaintext: %s' % plaintext
print 'Encrytped: %s' % encrypted
print 'Decrytped: %s' % decrypted