我正在编写一个程序,该程序用一个单词用英语将其翻译成虚构的语言,精灵。我需要做的是翻译一个句子,但是我不知道该怎么做。到目前为止,我的程序仅适用于单个单词。另外,我需要程序以以下句子开头:“ElcómewóótenhetenIgpénLvísheáránslátórtë!(欢迎使用猪精灵翻译!)”。
但是,我不知道如何在一开始显示它。我知道需要一系列循环,但是我不了解for / while循环,因为我认为自己能够做到这一点。最终,一旦程序运行并显示用户输出,我需要能够询问用户是否要将另一个单词翻译成精灵语,如果是,请再次遍历代码。
# User Input: Ask user for a word
WordToBeTranslated = input("Please enter a word you would like to translate: ")
WordToBeTranslatedLower = WordToBeTranslated.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)
newWord = input("Would you like to translate another word? (y/n): ")
答案 0 :(得分:0)
如果我理解正确,请尝试一下,这只是简单的代码安排。
首先,翻译器函数包装:
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
"""