# file: encoding.py # This program encodes a message using ASCII codes # # NOTE: chr("a") = 97 and chr("z") = 122. # When we shift we want the new value to fall within these limits. Otherwise we cycle (that is, 123 # corresponds to 97, etc, and 96 corresponds to 122) def main(): sentence = input("Enter sentence all in lowercase") sentence = sentence.lower() # Make sentence all lowercase (just in case) key = int(input("Enter key")) # Ask for key outpt = "" for ch in sentence: if ch == " ": # if character is a space, just leave the space where it is outpt = outpt + ch elif ord(ch) + key > 122: # if after shift we get above 122, subtract 26 to go back to "a" outpt = outpt + chr(ord(ch)+key - 26) elif ord(ch) + key < 97 : # if after shift we get below 97, add 26 to go back to "z" outpt = outpt + chr(ord(ch)+key + 26) else: outpt = outpt + chr(ord(ch)+key) print(outpt) main()