In: Computer Science
Create a message encoder/decoder.
PLEASE USE BASIC PYTHON METHODS/FUNCTIONS.
Please see the next page an example of the output.
#decode function
def encode():
message = input("Enter the message ") #taking message as input
shift = int(input("Enter the shift code ")) #taking shift code as input
if(shift < -26 or shift > 26): #check if shift is between -26 and 26
print("Please Enter a valid shift code ")
else:
encoded_values = [ord(ele) + shift for ele in message] #convert message to ascii and add shift code
encoded_message = [chr(x) for x in encoded_values] #convert ascii values to characters
encoded_message = "".join(encoded_message) #join the list of characters
print("The encoded message is: ", encoded_message) #print the encoded message
#decode function
def decode():
message = input("Enter the message ") #taking message as input
shift = int(input("Enter the shift code ")) #taking shift code as input
if(shift < -26 or shift > 26): #check if shift is between -26 and 26
print("Please Enter a valid shift code ")
else:
decoded_values = [ord(ele) - shift for ele in message] #convert message to ascii and add shift code
decoded_message = [chr(x) for x in decoded_values] #convert ascii values to characters
decoded_message = "".join(decoded_message) #join the list of characters
print("The decoded message is ", decoded_message) #print the decoded message
alphabets = [chr(i) for i in range(97,123)] #generating the alphabets from a to z
alphabets.append(" ") #appending space at the last
#printing the menu
while(True):
print("\n1. Encode a message") #choice 1
print("\n2. Decode a message") #choice 2
print("\n3. Quit") #choice 3
choice = int(input("Enter your choice: ")) #taking choice as the input
if(choice == 1): #encode if choice is 1
encode()
elif(choice == 2): #decode if choice is 2
decode()
elif(choice == 3): #quit if choice is 3
break
else:
print("Enter the valid choice") #if choice is not 1,2,3 print invalid choice
Code Screenshot
encode function
decode function
remaining code
Output Screenshot
Comments has been given for all the lines in the code. Also the screenshot of the complete code and output has been attached for the reference.