我正在编写一个程序,它接受一个英语单词,并将其翻译成想象中的语言精灵。我需要做的是翻译一个句子,但我不知道如何做到这一点。到目前为止,我的程序只适用于一个单词。另外,我需要程序以句子开头:"Elcómewóten heten Igpén Lvítheáránslátórtë!(欢迎使用Pig Elvish翻译器!)“。
但是,我不知道如何在开始时显示它。我知道需要一系列循环,但我不理解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): ")发布于 2019-02-04 06:28:56
如果我理解正确的话,试一下这个,这只是一个简单的代码安排。
首先,translator函数包装器:
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
"""https://stackoverflow.com/questions/54507808
复制相似问题