Skip to content

Instantly share code, notes, and snippets.

@TomDeBeauchamp
Last active December 19, 2015 02:08
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 TomDeBeauchamp/5880401 to your computer and use it in GitHub Desktop.
Save TomDeBeauchamp/5880401 to your computer and use it in GitHub Desktop.
This was my code for the Secret Messages problem from MIT's OCW Gentle Intro to Comp Sci via Python class, the first problem set, optional material.
letter = "a"
# converts a letter to ascii code
ascii_code = ord(letter)
# converts ascii code to a letter
letter_res = chr(ascii_code)
encoded_phrase = ""
phrase = raw_input("Type the phrase you'd like to see encoded. ")
shift = int(raw_input("Type the shift value. "))
for c in phrase:
if c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
c = ((ord(c)-65 + shift)%26)
c = chr(c%26+65)
elif c in "abcdefghijklmnopqrstuvwxyz":
c = ((ord(c)-97 + shift)%26)
c = chr(c%26+97)
encoded_phrase = encoded_phrase + c
print encoded_phrase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment