Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. # -----------------------------------------------------------------------------
  2. # Name: translate
  3. # Purpose: assignment # 3
  4. #
  5. # Author:
  6. # Date:
  7. # -----------------------------------------------------------------------------
  8. """
  9. Enter your docstring with a one-line overview here
  10.  
  11. and a more detailed description here.
  12. """
  13.  
  14.  
  15. def starts_with_vowel(word):
  16. """
  17. Enter your function docstring here
  18.  
  19. This function checks if word is greater than one, alphanumeric, lower case
  20. and a vowel
  21.  
  22. parameter : word (string)
  23.  
  24. """
  25. # return True if the word starts with a vowel and False otherwise
  26. # #if length of word is greater than 0 and word is alphanumeric
  27. if len(word) > 0 and word.isalpha():
  28. # Convert word to lowercase and transform into word_conversion variable
  29. word_conversion = word.lower()
  30. # The 1st letter you input as word transform into first_letter variable
  31. first_letter = word_conversion[0]
  32. # if the first letter is a vowel
  33. if first_letter in ('a', 'e', 'i', 'o', 'u'):
  34. return True
  35. else:
  36. return False
  37. else:
  38. print("empty")
  39.  
  40.  
  41. def encrypt(word):
  42. """
  43. Enter your function docstring here
  44. parameters : word (string)
  45. """
  46. # encrypt a single word into the secret language
  47. # call starts_with_vowel to decide which pattern to follow
  48. new_word_encrypt = starts_with_vowel(word)
  49. if new_word_encrypt is True:
  50. new_word_tan = word + 'tan'
  51. # return a single word (encrypted)
  52. return new_word_tan
  53. else:
  54. new_word_est = word[1:] + word[0] + 'est'
  55. # return a single word (encrypted)
  56. return new_word_est
  57. # return a single word (encrypted)
  58.  
  59.  
  60. def decrypt(word):
  61. """
  62. Enter your function docstring here
  63. parameters: word (string)
  64. """
  65. # decrypt a single word from the secret language
  66. if word[0] in ('a', 'e', 'i', 'o', 'u') and word[-3:] == 'tan':
  67. word_sans_tan = word[:-3]
  68. return word_sans_tan
  69. elif word[0] not in ('a', 'e', 'i', 'o', 'u') and word[-3] == 'est':
  70. word_sans_est = word[-4] + word + word[-4:]
  71. return word_sans_est
  72. else:
  73. # If the word is not a valid word in the secret language, return None
  74. print('None, invalid message')
  75.  
  76.  
  77. def translate(text, mode):
  78. """
  79. Enter your function docstring here
  80.  
  81. parameters: text (string)
  82. mode (string)
  83. word (string)
  84. translated message (string)
  85. encrypted_word (string)
  86. reversed_list (string)
  87. encrypted_list (string)
  88. """
  89. # Translate (encrypt or decrypt) the whole message
  90. # Split the text into a list of words
  91. list_words = text.split()
  92. # if mode is 'E' encrypt each of the words in the list
  93. translate_list = []
  94. if mode == "E":
  95. for word in list_words:
  96. encrypted_word = encrypt(word)
  97. # Build a new list with these translated words
  98. translate_list.append(encrypted_word)
  99.  
  100. # if mode id 'D' decrypt each word in the list
  101. if mode == "D":
  102. for word in list_words:
  103. decrypted_word = decrypt(word)
  104. # Build a new list with these translated words
  105. translate_list.append(decrypted_word)
  106.  
  107. translate_list.reverse()
  108. final_list = " ".join(translate_list)
  109. # and return it
  110. return final_list
  111. # final_transformation = reversed_list
  112. # return final_transformation
  113.  
  114.  
  115. def choose_mode():
  116. """
  117. Enter your function docstring here
  118. """
  119. # Prompt the user for input repeatedly until they enter 'E' or 'D'.
  120. while True:
  121. user_input = input("Please type E to encrypt or D to" +
  122. " decrypt a message:")
  123. # Return the user's choice.
  124. if user_input in ('E', 'D'):
  125. # Return the user's choice.
  126. return user_input
  127. else:
  128. print("Invalid Choice")
  129.  
  130.  
  131. def main():
  132. # Get the user choice 'E' or 'D' and save it in a variable.
  133. user_choice = choose_mode()
  134. # Prompt the user for the message to be translated.
  135. if user_choice in ('E', 'D'):
  136. message = input("Please enter your message:")
  137. translated_message = translate(message, user_choice)
  138. print(translated_message)
  139. else:
  140. print("Invalid Choice")
  141.  
  142.  
  143. if __name__ == '__main__':
  144. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement