Question

In: Computer Science

I want to make a python program that encodes/decodes inputted messages. There are only three options...

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.

Solutions

Expert Solution

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:


Related Solutions

Code in python Hello I want a program to read only the price of each line...
Code in python Hello I want a program to read only the price of each line in a txt file in python. The price is present right after the 2nd comma within the txt file. The output that I want is also printed at the bottom. Inside the txt file: 2019-08-01 00:08:39,BTC/USD,10128.08 2019-08-01 00:08:21,BTC/USD,10117.54 2019-08-01 00:08:20,BTC/USD,10120.51 2019-08-01 00:08:19,BTC/USD,10118.13 2019-08-01 00:08:18,BTC/USD,10120.74 Python file output: Price 1: 10128.08 Price 2: 10117.54 Price 3: 10120.51 Price 4: 10118.13 Price 5: 10120.74
Python: I want to make the following code to prompt the user if want to run...
Python: I want to make the following code to prompt the user if want to run the software again. I am new to python, so i do not know if it works like c++. If can explain that would be much appreciated base = float(input("Enter base of the triagle: ")) Triangle_Right = float(input("Enter right side of the triagle: ")) Triangle_Left = float(input("Enter left side of the triagle: ")) height = float(input("Enter the height of the triangle: ")) perimiter = base...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
In python using tkinter, I want to create a program. The program can have various classes...
In python using tkinter, I want to create a program. The program can have various classes and methods but I want it to have circles, triangles, and squares. Each shape movies in a certain way, like circles can move linearly, triangles can be affected by gravity, and squares can only go up and down. If the shapes get too close to one another then they disappear and a new shape appears somewhere else. I want this program to run continuously.
code in python I want to make a function that adds all the multipliers together. The...
code in python I want to make a function that adds all the multipliers together. The code is posted below and please look at wanted output section to see what I want the code to output. The last line of the code is the only addition I need. Thanks. Code: initial=int(input("Initial value : "))       #taking inputs multiplier=float(input("Multiplier : ")) compound=int(input("No of compounds : ")) print("Your values are:") mult=initial                         #initalizing to find answer for i in range(0,compound):         #multiplying by multiplier    ...
This program is supposed to identify whether I inputted a number or a letter through enumerated...
This program is supposed to identify whether I inputted a number or a letter through enumerated types and an array of messages. I'm aware that you can't compare strings and enums but I'm out of ideas. What method do you suggest. #include <iostream> #include <iomanip> using namespace std; int main() {    enum letters { 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,...
PYTHON (BEGINNER) program that allows the user to choose any of the three sports options described...
PYTHON (BEGINNER) program that allows the user to choose any of the three sports options described below and computes the relevant statistic in each case: Quidditch Score Total: Determined based on the number of goals and whether or not the snitch was caught. A goal is scored by propelling the quaffle through a hoop and each earns the team 10 points. If a team catches the snitch, that team earns an additional 30 points. The snitch can be caught at...
I am trying to make a program in Python that opens a file and tells you...
I am trying to make a program in Python that opens a file and tells you how many lines, vowels, consonants, and digits there are in the file. I got the print out of lines, digits, and consonants, but the if statement I made with the vowels list isn't recognizing them. How can I make the vowels go through the if statement with the list? Am I using the wrong operator? Here is my program #Assignment Ch7-1 #This program takes...
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
how can i make this program in python? Captain Øks is on his way to conquer...
how can i make this program in python? Captain Øks is on his way to conquer Mosefjell. He has a map of the area and he is in his camp at the designated place on the map. The map is divided into cells. For each cell, the height is given. Captain Øks is not happy, the landscape is full of hills, and he hates to climb both up and down. He has hired you to write a program that will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT