6.189 Exercise Opt.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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