我对编程还很陌生,我被分配了一项将英语文本转换为 Pig Latin 的家庭作业。
我到目前为止的代码是:
VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")
def vowel_start(word):
pig_latin = word + "ay"
return pig_latin
def vowel_index(word):
for i, letters in enumerate(word):
if letters in VOWELS:
vowel_index = i
pig_latin = word[vowel_index:] + word[:vowel_index] + "ay"
return pig_latin
else:
pig_latin = word #The issue here is that even if the word
return pig_latin #has vowels, the program will still only
#return the untranslated word as shown
#in else.
english_text = raw_input("What do you want to translate?")
translate = english_text.split()
pig_latin_words = []
translated_text = "".join(str(pig_latin_words)) #The issue here is that the list
#will not join with the string.
for i in translate:
first = i[0]
vow = False
if first in VOWELS:
vow = True
if vow == True:
pig_latin_words.append(vowel_start(i))
else:
pig_latin_words.append(vowel_index(i))
print "The text you translated is " + english_text
print "The translated text is " + translated_text #The issue here is that the program
#displays "The translated text is "
#and that's it
如果我注释掉 def vowel_index 函数的 else 方面,那么 if 方面就会起作用。如果我将它留在程序中,则 if 方面不再起作用。过去几天我一直在尝试解决这个问题,但我不知道如何解决它。任何帮助将不胜感激,谢谢!
有关作业的更多详细信息:
- 如果单词以元音开头,则保持单词原样并在末尾添加“ay”。
- 如果单词包含元音,但开头没有,则取元音之前的字母,将其移到单词的末尾并在末尾添加“ay”。
- 如果单词不包含任何元音,则保持单词原样。