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

C++ Text message decoder Use getline() to get a line of user input into a string:...
C++ Text message decoder Use getline() to get a line of user input into a string: Enter text: IDK if I'll go. It's my BFF's birthday. Search the string using find() for common abbreviations and replace() them with their long form. In addition, use a for loop to iterate through each character of the string and replace any occurences of '.' with '!': Enter text: IDK if I'll go. It's my BFF's birthday. I don't know if I'll go! It's...
**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...
Please create a Python program that can convert a text string to an encrypted message and...
Please create a Python program that can convert a text string to an encrypted message and convert the encrypted message back to the original message. Read a string message from your keyboard. Print the input string. Convert this message into an encrypted message by using a list element substitution. Print your encrypted message. Convert this encrypted message back to the original message by using a list element substitution. Print your original message. Important: please use a dictionary data type and...
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.
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
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...
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...
(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT