Home > Python >Python Exercise: Shift Code

Shift Code

A shift code is where a message can be easily encoded and is one of the simplest codes to use. Each letter is moved forwards through the alphabet a set number of letters to be represented by a new letter. For instance, “abc” becomes “bcd” when the code is shifted by one (i.e. each letter in the alphabet is moved forward one character and "z" move back to "a").

You need to create a program which will display the following menu:

  • 1) Encode a text message from user input
  • 2) Decode a text message from user input
  • 3) Quit the program.
If the user selects 1, they should be able to type in a message (including spaces) and then enter a number. Python should then display the encoded message once the shift code has been applied.

If the user selects 2, they should enter an encoded message and the correct number and it should display the decoded message (i.e. move the correct number of letters backwards through the alphabet).

If they select 3 it should stop the program from running.

After they have encoded or decoded a message the menu should be displayed to them again until they select quit.

Assumption

You can assume that the input text message only have lower letters and space.

The shifting will not apply to a space.

You need login to comment

Solution submitted by Dzeng at May 20, 2020, 12:16 a.m.
def encode (message, shift): encodedmessage = "" for i in message: if ord(i) == 32: encodedmessage = encodedmessage + chr(ord(i)) elif ord(i) + shift > 122: encodedmessage = encodedmessage + chr(ord(i) + shift - 26) else: encodedmessage = encodedmessage+ chr(ord(i) + shift) return encodedmessage def decode (message, shift): decodedmessage = "" for i in message: if ord(i) == 32: decodedmessage = decodedmessage + chr(ord(i)) elif ord(i) - shift < 97: decodedmessage = decodedmessage + chr(ord(i) - shift + 26) else: decodedmessage = decodedmessage + chr(ord(i) - shift) return decodedmessage while True: choose = int(input("1 for encode, 2 for decode, 3 for quit: ")) if choose == 3: print("Quitting") break elif choose == 1: messagetoencode = str(input("Enter the message you want to encode: ")) encodeshift = int(input("Enter the number of letters you want to shift your message by: ")) print(encode(messagetoencode, encodeshift)) elif choose == 2: messagetodecode = str(input("Enter the message you want to decode: ")) decodeshift = int(input("Enter the number of letters you want to shift your message by: ")) print(decode(messagetodecode, decodeshift))
Solution submitted by Andrew at May 25, 2020, 3:04 a.m.
import sys def encode(input): encoder = { "a" : "b", "b" : "c", "c" : "d", "d" : "e", "e" : "f", "f" : "g", "g" : "h", "h" : "i", "i" : "j", "j" : "k", "k" : "l", "l" : "m", "m" : "n", "n" : "o", "o" : "p", "p" : "q", "q" : "r", "r" : "s", "s" : "t", "t" : "u", "u" : "v", "v" : "w", "w" : "x", "x" : "y", "y" : "z", "z" : "a"} return encoder[input] def decode(input): decoder = { "a" : "z", "b" : "a", "c" : "b", "d" : "c", "e" : "d", "f" : "e", "g" : "f", "h" : "g", "i" : "h", "j" : "i", "k" : "j", "l" : "k", "m" : "l", "n" : "m", "o" : "n", "p" : "o", "q" : "p", "r" : "q", "s" : "r", "t" : "s", "u" : "t", "v" : "u", "w" : "v", "x" : "w", "y" : "x", "z" : "y", } return decoder[input] def userselection(): print("Do you want to") print("1) Encode a message") print("2) Decode a message") print("3) Quit") userselect = input() return userselect def organize(input): stringmaker = [] for i in input: stringmaker.append(encode(i)) shiftcode = "".join(stringmaker) return shiftcode def dorganize(input): stringmaker = [] for i in input: stringmaker.append(decode(i)) shiftcode = "".join(stringmaker) return shiftcode def code(): a = userselection() while ( a != "3"): if a == "1": userinput = input("Input your message: ") validator = isinstance(userinput, str) if validator == False: sys.quit() print(organize(userinput)) print() print() elif a == "2": userinput = input("Decode your message: ") validator = isinstance(userinput, str) if validator == False: sys.quit() print(dorganize(userinput)) print() print() else: print("That is not a valid option") print() a = userselection() sys.exit() code()

You need login to submit your solution