def encrypt(s, n): result = "" for c in s.lower(): if 'a' <= c and c <= 'z': nextValue = ord(c) + n if nextValue > ord('z'): # FIXME: what if n is much greater than 26? nextValue -= 26 result += chr(nextValue) else: result += c return result # hmm... looks redundant compared to encrypt() def decrypt(s, n): result = "" for c in s.lower(): if 'a' <= c and c <= 'z': nextValue = ord(c) - n if nextValue < ord('a'): # FIXME: what if n is much less than 26? nextValue += 26 result += chr(nextValue) else: result += c return result encode = raw_input("Encrypt or Decrypt? (E/D) ") msg = raw_input("What is the message? ") rot = input("How many rotations? ") if encode == 'E': # requires precise input from user print "Here's the ciphertext:", encrypt(msg, rot) elif encode.startswith('D'): # user can input "Decrypt" or "Don't do it!" but not "decrypt" print "Here's the plaintext:", decrypt(msg, rot)