Advertisement
KillianMills

caesar.py

Aug 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1.  
  2. MAX_KEY_SIZE = 26
  3.  
  4.  
  5. def getMode():
  6.     while True:
  7.         print('Do you wish to encrypt or decrypt a message?: ')
  8.  
  9.        
  10.         mode = input().lower()
  11.        
  12.        
  13.         if mode in 'encrypt e decrypt d'.split():
  14.             return mode
  15.         else:          
  16.             print('Enter either "Encrypt" or "E" or "Decrypt" or "D" ')
  17.  
  18. def getMessage():
  19.     print('Enter your message: ')
  20.     return input()
  21.  
  22. def getKey():
  23.      key = 0
  24.      while True:
  25.          print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
  26.          key = int(input())
  27.          if (key >= 1 and key <= MAX_KEY_SIZE):
  28.              return key
  29.                        
  30. def getTranslatedMessage(mode, message, key):
  31.  
  32.     translated = []
  33.  
  34.     if mode[0] == 'd':
  35.          key = -key
  36.            
  37.     for symbol in message:
  38.         print(symbol)
  39.         num = 0
  40.  
  41.         # is it a letter
  42.         if symbol.isalpha():
  43.  
  44.             # is it upper case
  45.             if symbol.isupper():
  46.                 num = ord(symbol)
  47.                 print(num)
  48.                 # for wrapping around the alphabet
  49.                 if num > ord ('Z'):
  50.                     num -= 26
  51.                     print('Z ', num)
  52.                 elif num < ord('A'):
  53.                     num += 26
  54.                     print('A ', num)
  55.  
  56.                 num = ord(symbol)
  57.                 num += key
  58.                 translated.append(num)
  59.  
  60.  
  61.             # is it lower case
  62.             elif symbol.islower():
  63.                 if num > ord('z'):
  64.                     num -= 26
  65.                     print('z ', num)
  66.                 elif num < ord('a'):
  67.                     print('num' , num)
  68.                     print('ord ', ord('a'))
  69.                     num += 26
  70.                     print(symbol)
  71.                     print('a ', num)
  72.  
  73.                 num = ord(symbol)
  74.                 num += key
  75.                 translated.append(num)
  76.  
  77.  
  78.         # else add to the translated list
  79.         else:
  80.             translated.append(symbol)
  81.  
  82.  
  83.             # add translated letter to list for lower
  84.  
  85.     # after we have gone through the word, we return the translate
  86.     return translated
  87.  
  88.  
  89. print("getMode()")
  90. mode = getMode()
  91. print("getMode: ", mode)
  92. print("getMessage()")
  93. message = getMessage()
  94. print("getMessage: ", message)
  95. print("getKey()")
  96. key = getKey()
  97. print("key: ", key)
  98. print('Your translated text is: ')
  99. result = getTranslatedMessage(mode,message,key)
  100. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement