Question

In: Computer Science

Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could...

Create a message encoder/decoder.

PLEASE USE BASIC PYTHON METHODS/FUNCTIONS.

  • The user enters a message that could only include alphabetic letters and space.
    • There are 26 alphabetic letters. Consider space the 27th letter.
  • The user then enters a shift code that should be an integer between -26 and 26.
  • The application will show the encoded/decoded message based on the shift code entered.
    • If you encode a message, each letter in the message will be moved forward through the alphabet according to your shift code.
      • For example, if the message is “abcd” and the shift code is 1, the encoded message will be “bcde”.
    • If you decode a message, each letter in the message will be moved backward through the alphabet according to your shift code.
      • For example, if the message is “abcd” and the shift code is 1, the decoded message will be “ abc”.
  • The application should display the following menu:
    • 1. Encode a message
    • 2. Decode a message
    • 3. Quit
  • If the user selects 1, he/she should be able to type in a message and then enter a shift code. The application should then display the encoded message.
  • If the user selects 2, he/she should be able to type in a message and then enter a shift code. The application should then display the decoded message.
  • After the user has encoded/decoded a message, the menu should be displayed again until the user selects 3.
    • When the user selects from the menu, if he/she enters anything but 1, 2, or 3, the application should tell the user his/her selection is invalid.
    • If the user enters a shift code greater than or equal to 27 (or less than or equal to -27), the application should tell the user to enter a valid shift code (between -26 and 26).

Please see the next page an example of the output.

  • I first entered an invalid selection (i.e., ‘a’).
  • Then, while trying to encode a message, I entered an invalid shift code (i.e., 30).
  • Then, I decoded a message.
  • Finally, I stopped (entering 3).

Solutions

Expert Solution

#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.


Related Solutions

**Use C** You will write an encoder and a decoder for a modified "book cipher." A...
**Use C** You will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the text. The first number...
**Use C** You will write an encoder and a decoder for a modified "book cipher." A...
**Use C** You will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the text. The first number...
Using Visual Basic 2017, Create a form with two textboxes and one button. A user enters...
Using Visual Basic 2017, Create a form with two textboxes and one button. A user enters the course she is taking in the first text box and then clicks on the button. Then if the user entered "Program Language" in the first text box, the second text box will show "Awesome pick!". If the user entered something else, the text box shows "Ok." Please write the correct code.
Program must use Python 3 Your program must have a welcome message for the user. Your...
Program must use Python 3 Your program must have a welcome message for the user. Your program must have one class called CashRegister. Your program will have an instance method called addItem which takes one parameter for price. The method should also keep track of the number of items in your cart. Your program should have two getter methods. getTotal – returns totalPrice getCount – returns the itemCount of the cart Your program must create an instance of the CashRegister...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The program will use a while loop to let the program keep running. The user will be allowed to use the program as long as the quitVariable is equal to 'y' or 'Y'. When the user decides to quit playing, say "Thanks for playing!". The program will use a for loop to test if the number are even or odd. If even print "number is...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and a menu of options for the user to choose from. Welcome to the Email Analyzer program. Please choose from the following options: Upload text data Find by Receiver Download statistics Exit the program Program Options Option 1: Upload Text Data If the user chooses this option, the program will Prompt the user for the file that contains the data. Read in the records in...
03 Prepare : Checkpoint B Objective Demonstrate basic class methods (member functions) in Python. After completing...
03 Prepare : Checkpoint B Objective Demonstrate basic class methods (member functions) in Python. After completing (or while completing) the preparation material for this week, complete the following exercise. Introduction Recall from your mathematics classes that a complex number is composed of two parts, a real part and an imaginary part. We could write a complex number in the form "3 + 4i" where 3 is the real part and 4 is the imaginary part. For this program, we will...
Create a simple python app that allows the user to create a roster of students and...
Create a simple python app that allows the user to create a roster of students and their grade on CUS-1166. Moreover the app needs to calculate the average grade of students added to the roster. 1-To begin with, create a new file n the same working folder as part A (i.e. cus1166_lab1) and name it app.py. Moreover, create a subfolder and name it mymodules. 2-Within mymodules create the files __init__.py , models.py , math_utils.py . 3-In the models.py file define...
Please write in python Use modular design to write a program that asks the user to...
Please write in python Use modular design to write a program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet. PLANET CONVERSION FACTOR Mercury 0.4155 Venus 0.8975 Earth 1.0000 Moon 0.1660 Mars 0.3507 Jupiter 2.5374 Saturn 1.0677 Uranus 0.8947 Neptune 1.1794 Pluto 0.0899 The...
3.24 LAB C++ : Program: Text message decoder (1) Use getline() to get a line of...
3.24 LAB C++ : Program: Text message decoder (1) Use getline() to get a line of user input into a string. Output the line. (3 pts) Ex: Enter text: IDK if I'll go. It's my BFF's birthday. You entered: IDK if I'll go. It's my BFF's birthday. (2) Search the string (using find()) for common abbreviations and print a list of each found abbreviation along with its decoded meaning. (3 pts) Ex: Enter text: IDK if I'll go. It's my...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT