Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -----------------------------------------------------------------------------
- # Name: translate
- # Purpose: assignment # 3
- #
- # Author:
- # Date:
- # -----------------------------------------------------------------------------
- """
- Enter your docstring with a one-line overview here
- and a more detailed description here.
- """
- def starts_with_vowel(word):
- """
- Enter your function docstring here
- This function checks if word is greater than one, alphanumeric, lower case
- and a vowel
- parameter : word (string)
- """
- # return True if the word starts with a vowel and False otherwise
- # #if length of word is greater than 0 and word is alphanumeric
- if len(word) > 0 and word.isalpha():
- # Convert word to lowercase and transform into word_conversion variable
- word_conversion = word.lower()
- # The 1st letter you input as word transform into first_letter variable
- first_letter = word_conversion[0]
- # if the first letter is a vowel
- if first_letter in ('a', 'e', 'i', 'o', 'u'):
- return True
- else:
- return False
- else:
- print("empty")
- def encrypt(word):
- """
- Enter your function docstring here
- parameters : word (string)
- """
- # encrypt a single word into the secret language
- # call starts_with_vowel to decide which pattern to follow
- new_word_encrypt = starts_with_vowel(word)
- if new_word_encrypt is True:
- new_word_tan = word + 'tan'
- # return a single word (encrypted)
- return new_word_tan
- else:
- new_word_est = word[1:] + word[0] + 'est'
- # return a single word (encrypted)
- return new_word_est
- # return a single word (encrypted)
- def decrypt(word):
- """
- Enter your function docstring here
- parameters: word (string)
- """
- # decrypt a single word from the secret language
- if word[0] in ('a', 'e', 'i', 'o', 'u') and word[-3:] == 'tan':
- word_sans_tan = word[:-3]
- return word_sans_tan
- elif word[0] not in ('a', 'e', 'i', 'o', 'u') and word[-3] == 'est':
- word_sans_est = word[-4] + word + word[-4:]
- return word_sans_est
- else:
- # If the word is not a valid word in the secret language, return None
- print('None, invalid message')
- def translate(text, mode):
- """
- Enter your function docstring here
- parameters: text (string)
- mode (string)
- word (string)
- translated message (string)
- encrypted_word (string)
- reversed_list (string)
- encrypted_list (string)
- """
- # Translate (encrypt or decrypt) the whole message
- # Split the text into a list of words
- list_words = text.split()
- # if mode is 'E' encrypt each of the words in the list
- translate_list = []
- if mode == "E":
- for word in list_words:
- encrypted_word = encrypt(word)
- # Build a new list with these translated words
- translate_list.append(encrypted_word)
- # if mode id 'D' decrypt each word in the list
- if mode == "D":
- for word in list_words:
- decrypted_word = decrypt(word)
- # Build a new list with these translated words
- translate_list.append(decrypted_word)
- translate_list.reverse()
- final_list = " ".join(translate_list)
- # and return it
- return final_list
- # final_transformation = reversed_list
- # return final_transformation
- def choose_mode():
- """
- Enter your function docstring here
- """
- # Prompt the user for input repeatedly until they enter 'E' or 'D'.
- while True:
- user_input = input("Please type E to encrypt or D to" +
- " decrypt a message:")
- # Return the user's choice.
- if user_input in ('E', 'D'):
- # Return the user's choice.
- return user_input
- else:
- print("Invalid Choice")
- def main():
- # Get the user choice 'E' or 'D' and save it in a variable.
- user_choice = choose_mode()
- # Prompt the user for the message to be translated.
- if user_choice in ('E', 'D'):
- message = input("Please enter your message:")
- translated_message = translate(message, user_choice)
- print(translated_message)
- else:
- print("Invalid Choice")
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement