【发布时间】:2013-02-12 06:52:48
【问题描述】:
我对编程还很陌生,我被分配了一项将英语文本转换为 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”。
- 如果单词不包含任何元音,则保持单词不变。
【问题讨论】:
-
你遇到了什么错误?
-
在 translate_text 的行上,join 将一个列表作为参数,你传递给它一个字符串。
-
translated_text应该是什么?它是加入空列表的空字符串,并且在打印出来的最后一行之前,您永远不会再次引用它。它将保持为空。
标签: python list if-statement