Question: (PYTHON) **PLEASE USE THE TEMPLATE PROVIDED** Your task is to write a Python program that encrypts or decrypts a given text into/from a secret language.
(PYTHON) **PLEASE USE THE TEMPLATE PROVIDED**
Your task is to write a Python program that encrypts or decrypts a given text into/from a secret language.
Individual words in the text are encrypted according to the following rules:
If the word starts with a vowel append 'tan' to the word. The vowels are a, e, i, o, u.
Otherwise, take the first letter of the word, move it to the end and then append 'est'.
So:
apple becomes appletan
orange becomes orangetan
banana becomes ananabest
python becomes ythonpest
For simplicity we'll assume that the input text is in lowercase and that it does not contain any punctuation (no commas, periods, etc...)
Your program will first ask the user to make a choice between encryption into the secret language (E or e) or decryption from the secret language (D or d).
When the user specifies 'E' or 'e' for encryption, the program will translate each word in the input text into the secret language, will put the translated words in reverse order into one translated message and will print out the translated message. The translated message will have exactly one space character between words.
When the user specifies 'D' or 'd' for decryption', the program will attempt to recover the original words from the input text. This is only possible if the words have a specific format: they start with a vowel and end with tan or they end with a consonant followed by est. If one or more word does not have this format, the decryption fails and the message 'Invalid message.' is printed.
Testing:
Make sure that you test your solution before you submit it. Here are a few test cases with the expected output. Feel free to add your own.
Test case 1 - encryption is correct for words starting with a vowel
Please type E to encrypt or D to decrypt a message: E
Please enter your message: apple
The secret message is: appletan
Test case 2 - encryption is correct for words starting with a consonant (non vowel)
Please type E to encrypt or D to decrypt a message: e
Please enter your message: banana
The secret message is: ananabest
Test case 3 - encryption is correct for phrases with multiple words
Please type E to encrypt or D to decrypt a message: e
Please enter your message: python is fun
The secret message is: unfest istan ythonpest
Test case 4 - encryption is correct when words are separated by more than one space character
Please type E to encrypt or D to decrypt a message: E
Please enter your message: simple is better than complex
The secret message is: omplexcest hantest etterbest istan implesest
Test case 5 - decryption is correct for words starting with a vowel and ending with 'tan'
Please type E to encrypt or D to decrypt a message: D
Please enter your message: orangetan
The secret message is: orange
Test case 6 - decryption is correct for words ending with a consonant followed with 'est'
Please type E to encrypt or D to decrypt a message: d
Please enter your message: ryptographycest
The secret message is: cryptography
Test case 7 - decryption is correct for phrases with multiple words
Please type E to encrypt or D to decrypt a message: D Please enter your message: omplexcest hantest etterbest istan implesest The secret message is: simple is better than complex
Test case 8 - A message containing one or more invalid words is not decrypted
Please type E to encrypt or D to decrypt a message: d Please enter your message: omplexcest hantest etterbest you istan super implesest Invalid message.
Test case 9 - Invalid words ending with tan are not decrypted
Please type E to encrypt or D to decrypt a message: D Please enter your message: suntan Invalid message.
Test case 10 - Invalid words ending with est are not decrypted
Please type E to encrypt or D to decrypt a message: d Please enter your message: silliest Invalid message.
Test case 11 - tan is not decrypted
Please type E to encrypt or D to decrypt a message: D Please enter your message: tan Invalid message.
Test case 12 - est is not decrypted
Please type E to encrypt or D to decrypt a message: d Please enter your message: tan Invalid message.
Test case 13 - Short words do not cause the program to crash
Please type E to encrypt or D to decrypt a message: D Please enter your message: m Invalid message.
Test case 14 - Short words do not cause the program to crash
Please type E to encrypt or D to decrypt a message: d Please enter your message: i Invalid message.
Test case 15 - The user is repeatedly prompted for input until they enter a valid choice (E, e, D or d)
Please type E to encrypt or D to decrypt a message: K Invalid choice. Please type E to encrypt or D to decrypt a message: x Invalid choice. Please type E to encrypt or D to decrypt a message: e Please enter your message: hello world The secret message is: orldwest ellohest
(PLEASE FOLLOW THE TEMPLATE BELOW)
def starts_with_vowel(word):
"""
Enter your function docstring here
Make sure you list the parameter and return value and their expected
types
"""
# return True if the word starts with a vowel and False otherwise
def encrypt(word):
"""
Enter your function docstring here
Make sure you list the parameter and return value and their expected
types
"""
# encrypt a single word into the secret language
# call starts_with_vowel to decide which pattern to follow
# return a single word (encrypted)
def decrypt(word):
"""
Enter your function docstring here
Make sure you list the parameter and return value and their expected
types
"""
# decrypt a single word from the secret language
# if the word is not a valid word in the secret language, return None
def translate(text, mode):
"""
Enter your function docstring here
Make sure you list the parameters and return value and their expected
types
"""
# Translate (encrypt or decrypt) the whole message
# Split the text into a list of words
# if mode is E/e encrypt each of the words in the list
# if mode id D/d decrypt each word in the list
# Build a new list with these translated words
# Reverse the list
# join the list of reversed translated words into a single string
# and return it
def choose_mode():
"""
Enter your function docstring here
Make sure you list the return value and its expected type
"""
# Prompt user for input repeatedly until they enter 'E', 'e', 'D' or
'd'.
# Return the user's choice.
def main():
# Get the user choice 'E' or 'D' and save it in a variable.
# Prompt the user for the message to be translated.
# Translate the message by calling translate - save result.
# Print the result - or 'Invalid message.' if applicable.
if __name__ == '__main__':
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
