Если я правильно понял, попробуйте это просто кодовое расположение.
Сначала обертка функции переводчика:
def elvish_translator(input_sentence):
WordToBeTranslatedLower = input_sentence.lower()
# Condition #1: Moving the First Letter to the end
elvish = WordToBeTranslatedLower[1:] + WordToBeTranslatedLower[0]
# Condition #2 + #3: Appending a Vowel / Appending 'en' to the end of a word
vowel = ['a', 'e', 'e', 'i', 'o', 'u']
import random
randomVowel = random.choice(vowel)
list = []
list.append(WordToBeTranslated)
if len(WordToBeTranslated) >= 4:
elvish += randomVowel
else:
elvish = elvish + 'en'
# Condition #4: change all k's to c's
elvish = elvish.replace('k', 'c')
# Condition #5: Replace 'e' at end of the word with ë
if elvish[-1] == 'e':
elvish = elvish[:-1] + 'ë'
# Condition #6: Capitalization
# Part of Condition #6 was achieved in Condition #1
elvishFinal = elvish.capitalize()
print(" \"" + WordToBeTranslated + "\"", "in elvish is:", elvishFinal)
Затем функция пользовательского ввода, которая продолжает спрашивать пользователя.y / n и только y / n:
def user_choice_function():
input_user = input("\nWould you like to translate another word? (y/n): ")
while input_user not in ['y', 'n']:
input_user = input(" Input error. Please choose between yes (y) or no (n). Try again: ")
return input_user
И, наконец, главное:
print("Elcómewó óten heten Igpén Lvísheá ránslátórtë! (Welcome to the Pig Elvish translator!)\n")
user_choice = 'y'
while user_choice == 'y':
WordToBeTranslated = input("Please enter a word you would like to translate: ")
elvish_translator(WordToBeTranslated)
user_choice = user_choice_function()
Пример:
"""
Elcómewó óten heten Igpén Lvísheá ránslátórtë! (Welcome to the Pig Elvish translator!)
Please enter a word you would like to translate: Hello world!
"Hello world!" in elvish is: Ello world!hu
Would you like to translate another word? (y/n): yes
Input error. Please choose between yes (y) or no (n). Try again: y
Please enter a word you would like to translate: Hello StackOverflow!
"Hello StackOverflow!" in elvish is: Ello staccoverflow!hu
Would you like to translate another word? (y/n): n
"""