Skip to content

Instantly share code, notes, and snippets.

@pdpinch
Created September 29, 2012 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdpinch/3802851 to your computer and use it in GitHub Desktop.
Save pdpinch/3802851 to your computer and use it in GitHub Desktop.
6.189 Exercise Opt.2
# OPT.2 from http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/assignments/MIT6_189IAP11_hw1.pdf
# Secret Messages
phrase = raw_input("Enter sentence to encrypt: ")
shift = input("Enter shift value: ")
encoded_phrase = ''
for c in phrase:
ascii = ord(c)
if 97 <= ascii < (97+26):
encoded_ascii = ascii - 97 # shift to zero
encoded_ascii = encoded_ascii + shift # shift up
encoded_ascii = encoded_ascii % 26 # loop the shift if it goes past z
encoded_ascii = encoded_ascii + 97 # shift back up to letter range
elif 65 <= ascii < (65+26):
encoded_ascii = ascii - 65 # shift to zero
encoded_ascii = encoded_ascii + shift # shift up
encoded_ascii = encoded_ascii % 26 # loop the shift if it goes past z
encoded_ascii = encoded_ascii + 65 # shift back up to letter range
else:
# do nothing; if it's not a letter it passes
encoded_ascii = ascii
encoded_c = chr(encoded_ascii)
encoded_phrase = encoded_phrase + encoded_c
print "The encoded phrase is: " + encoded_phrase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment