Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 6.189 Homework 1 Exercise OPT.2 – Secret Messages
- # A simple shift cypher
- # Done in Python 3.2.2
- mode = -1
- shift = 0
- message_in = ''
- message_out = ''
- while mode:
- try:
- mode = int(input("0 to Quit\n1 to Encode\n2 to Decode\n :"))
- if mode < -1 or mode > 2:
- print ("Not a valid selection.\n")
- mode = -1
- else: break
- except:
- print ("Not a valid selection.\n")
- if mode==0:
- print("Bye Bye!")
- exit(0)
- else:
- while shift==0:
- try:
- shift = int(input("Enter a shift value from 1 to 50: "))
- if shift < 1 or mode > 50:
- print ("Not a valid selection.\n")
- shift = 0
- else: break
- except:
- print ("Not a valid selection.\n")
- message_in = input("What is the message to encode? :")
- print("You typed:", message_in)
- for letter in message_in:
- temp = ord(letter)
- if mode == 1:
- if temp >= shift:
- message_out=message_out+chr(temp-shift)
- else:
- message_out=message_out+chr(temp-shift+126)
- else:
- if temp+shift < 127:
- message_out=message_out+chr(temp+shift)
- else:
- message_out=message_out+chr(temp+shift-126)
- print("En/De-codes to:", message_out)
Advertisement
Add Comment
Please, Sign In to add comment