我对编程很新,我被分配了一个家庭作业,将英文文本转换为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方面不再起作用。 过去几天我一直试图解决这个问题,我不知道如何解决这个问题。 任何帮助将不胜感激,谢谢!
有关作业的更多详细信息:
答案 0 :(得分:5)
当前函数的编写方式,您将始终根据第一个字符是否为元音返回for循环的第一次迭代。如果所有字符都不是元音,则需要遍历每个字符并仅返回单词。
首先删除else
;当你看到一个不是元音的单个字符时你不想返回,因为后面的字符可能是一个元音。既然你的return
中有一个if
语句,你知道如果你到达循环的末尾而没有返回word
中没有一个字符是元音,那么你可以只需在for循环之外返回单词。例如:
def vowel_index(word):
for i, letters in enumerate(word):
if letters in VOWELS:
vowel_index = i
return word[vowel_index:] + word[:vowel_index] + "ay"
return word
对于你的第二个问题,这里有几件事情。
首先,您需要移动translated_text
的创建,以便在pig_latin_words
已经拥有新单词之后。这将在你最后的印刷陈述之前。
此外,要将单词列表转换为由空格分隔的单个字符串,您应该使用以下内容:
translated_text = " ".join(pig_latin_words)
以下是一个显示差异的简短示例:
>>> pig_latin_words = ['atcay', 'ogday']
>>> print "".join(str(pig_latin_words)) # your version
['atcay', 'ogday']
>>> print " ".join(pig_latin_words) # my version
atcay ogday
答案 1 :(得分:2)
第一个问题:
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
return pig_latin
如果你有'BA'这个词,你的循环会在第一次将字母设置为'B',然后执行else,然后返回单词。在返回任何东西之前,你需要先寻找第一个元音。
你的第二个问题:
translated_text = "".join(str(pig_latin_words))
这应该是:
translated_text = "".join(pig_latin_words)
这是因为join需要一个列表,并且你传递了一个字符串。我还注意到translated_text设置为空字符串。你需要在该列表中添加一些内容。
答案 2 :(得分:2)
其他答案解决了您的第一个问题。我认为你可以通过移动这条线来解决你的第二个和第三个问题
translated_text = "".join(str(pig_latin_words))
在for循环之后:
pig_latin_words = []
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))
translated_text = " ".join(pig_latin_words) #The issue here is that the list
#will not join with the string.
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
正如其他地方提到的那样,您也可以从str()
左右删除pig_latin_words
。我还认为你想用空格而不是emtpy字符串加入它们:" ".join()