Advertisement
DragonOsman

caesar.py

Feb 20th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. import cs50
  2. import sys
  3.  
  4. if len(sys.argv) == 1 or len(sys.argv) > 2:
  5.     print("Usage: python caesar.py <key>")
  6.     exit(1)
  7.  
  8. print("plaintext: ", end="")
  9. plaintext = cs50.get_string()
  10. key = int(sys.argv[1])
  11. size = len(plaintext)
  12. ciphertext = []
  13.  
  14. if key >= pow(2, 31) - 26 or key <= 0:
  15.     print("The value for the key is invalid!")
  16. else:
  17.     if key > 26:
  18.         key %= 26
  19.     for i in range(size):
  20.         if plaintext[i].isalpha():
  21.             c = (ord(plaintext[i]) + key)
  22.             if plaintext[i].upper() > 'Z':
  23.                 c -= 26
  24.         ciphertext.append(chr(c))
  25.  
  26. print("ciphertext: ", end="")
  27. print("".join(ciphertext))
  28. exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement