In: Computer Science
I want to make a python program that encodes/decodes inputted messages.
There are only three options in the initial menu: 1. encode, 2. decode, 3. quit, and if 1, 2, or 3 are not the input the program says that it is not valid, try again.
If encode is chosen, the user inputs their message, the message should only be alphabetical and spaces. Then, the user can pick a number in the range [-27,27] with space being the 27th character (if the number is not in this range they should be reminded of the range and asked to try again) . The message is then "encoded" according to the number chosen by moving each character forward (or backward if negative) that many letters. For example, if the message is "apple pie" and the number inputted is 3, the encoded message will be "dssohcslh". If decode is selected, the user can put in the same message ("dssohcslh") and number, 3, and this message will be "apple pie" again. After encoding or decoding the initial menu should pop up again.
I would appreciate if you can try to keep it simple as I am a beginner. Inputted messages don't need to be saved in any way.
Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code. Please get back to me if you need any change in code. Else please upvote
CODE:
#Declare a list of albhabets and space as the 27th letter
alphabet_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
#Function that reads the shift factor from user, validate it and return a valid shift factor
def get_shift_factor():
isValid = False
while not isValid:
shift = int(input("Enter a number in range [-27, 27]: ")) #Reading the shift factor from user and convert to int
if(shift >= -27 and shift <= 27): #Check if shift factor entered is in range [-27, 27]
isValid = True #If yes, set isValid as True
else:
print("Number entered is out of range [-27, 27]... Please re-enter") #Else display error
return shift #return the shift factor
#This function should accept a string to encrypt and the shift factor to use.
#It should construct and return the corresponding encrypted message
def encrypt(message, shift):
encrypted_msg = "" #declare an empty string for encrypted_msg
for i in range(len(message)): #loop through each character in message
index = alphabet_list.index(message[i]) #get the index value of character in alphabet_list
#Adding the shift factor to index value of character
if((index + shift) > 26):
index = (index + shift) - 27
else:
index = index + shift
# append albhabet at index to encrypted_msg
encrypted_msg += alphabet_list[index]
return encrypted_msg #return the encrypted_msg
#This function should accept a string to decrypt and the shift factor to use.
#It should construct and return the corresponding decrypted (original) message.
def decrypt(message, shift):
decrypted_msg = "" #declare an empty string for decrypted_msg
for i in range(len(message)): #loop through each character in message
index = alphabet_list.index(message[i]) #get the index value of character in alphabet_list
#Subtracting the shift factor from index value of character
if((index - shift) < 0):
index = 27 + (index - shift)
else:
index = index - shift
#append albhabet at index to decrypted_msg
decrypted_msg += alphabet_list[index]
return decrypted_msg #return the decrypted_msg
#main function
def main():
repeat = True #set repeat as True
while repeat: #loop untill repeat is True
#displaying the menu
print("\nEnter 1 to encode a message")
print("Enter 2 to deode a message")
print("Enter 3 to quit the program")
choice = int(input("Enter your choice: ")) #reading the choice
#If choice is 1, encrypt a message
if choice == 1:
message = input("\nEnter the string to encode: ") #reading string to encrypt
shift_factor = get_shift_factor() #Getting the shift factor
encrypted_msg = encrypt(message, shift_factor) #calling encrypt() and get the encrypted_msg
print("The encoded message is", encrypted_msg) #displaying the encrypted_msg
#If choice is 2, decrypt a message
elif choice == 2:
message = input("\nEnter the string to decode: ") #reading string to decrypt
shift_factor = get_shift_factor() #Getting the shift factor
decrypted_msg = decrypt(message, shift_factor) #calling decrypt() and get the decrypted_msg
print("The decoded message is", decrypted_msg) #displaying the decrypted_msg
elif choice == 3:
print("\nGoodbye!!!") #displaying final message
repeat = False #set repeat as False
else:
print("Invalid entry...Please select your choice correctly!!!")
main() #calling main function
OUTPUT: