e_mccormick

6.189 Homework 1 Exercise OPT.2 – Secret Messages

Jun 20th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. # 6.189 Homework 1 Exercise OPT.2 – Secret Messages
  2. # A simple shift cypher
  3. # Done in Python 3.2.2
  4.  
  5. mode = -1
  6. shift = 0
  7. message_in = ''
  8. message_out = ''
  9.  
  10. while mode:
  11.     try:
  12.         mode = int(input("0 to Quit\n1 to Encode\n2 to Decode\n :"))
  13.         if mode < -1 or mode > 2:
  14.             print ("Not a valid selection.\n")
  15.             mode = -1
  16.         else: break
  17.     except:
  18.         print ("Not a valid selection.\n")
  19.  
  20. if mode==0:
  21.     print("Bye Bye!")
  22.     exit(0)
  23. else:
  24.     while shift==0:
  25.         try:
  26.             shift = int(input("Enter a shift value from 1 to 50: "))
  27.             if shift < 1 or mode > 50:
  28.                 print ("Not a valid selection.\n")
  29.                 shift = 0
  30.             else: break
  31.         except:
  32.             print ("Not a valid selection.\n")
  33.  
  34.  
  35. message_in = input("What is the message to encode? :")
  36.  
  37. print("You typed:", message_in)
  38.  
  39. for letter in message_in:
  40.     temp = ord(letter)
  41.     if mode == 1:
  42.         if temp >= shift:
  43.             message_out=message_out+chr(temp-shift)
  44.         else:
  45.             message_out=message_out+chr(temp-shift+126)
  46.     else:
  47.         if temp+shift < 127:
  48.             message_out=message_out+chr(temp+shift)
  49.         else:
  50.             message_out=message_out+chr(temp+shift-126)
  51.  
  52. print("En/De-codes to:", message_out)
Advertisement
Add Comment
Please, Sign In to add comment