Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. # Cipher
  2. # name: DM
  3. # Date: 1-19-2017
  4.  
  5. import string
  6.  
  7. #define alphabets
  8. l_case = string.ascii_lowercase
  9. u_case = string.ascii_uppercase
  10. alphabet = l_case + u_case
  11.  
  12. print ("************* Optional Exercise: Cipher ***********")
  13.  
  14. # Request user input for encoded phrase & shift value
  15. phrase = str(input("Please enter a phrase to encode: "))
  16. shift_value = int(input("Please enter a shift value: "))
  17.  
  18. # Set up empty string in which to put results of encryption
  19. encoded_phrase = ""
  20.  
  21. # Ascii for reference:
  22. #
  23. # A = 65 a = 97
  24. # Z = 90 z = 122
  25.  
  26. for c in phrase:
  27. #set up similar cases for both upper & lower case alphabets, making sure to change back the type from ord to chr
  28. if c in l_case:
  29. o = ord(c)
  30. # in cases where the shift would 'run over' the end of the ascii l_case alphabet, which terminates at z = 122, need to use the distance between the 'end of the line' (122) and the character to be encrypted.
  31. if o + shift_value > 122:
  32. # use d = 123 - o instead of d = 122 - o to be inclusive of 122
  33. d = 123 - o
  34. o = 97 + d
  35. encoded_phrase += chr(o)
  36.  
  37. else:
  38. encoded_phrase += chr(o + shift_value)
  39. if c in u_case:
  40. y = ord(c)
  41. if y + shift_value > 90:
  42. d = 91 - y
  43. y = 65 + d
  44. encoded_phrase += chr(y)
  45. else:
  46. encoded_phrase += chr(y + shift_value)
  47. elif c not in alphabet:
  48. encoded_phrase += c
  49.  
  50. print (encoded_phrase)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement