5

我对编程还很陌生,我被分配了一项将英语文本转换为 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”。
  • 如果单词不包含任何元音,则保持单词原样。
4

3 回答 3

5

根据当前编写函数的方式,您将始终根据第一个字符是否为元音来返回 for 循环的第一次迭代。您需要遍历每个字符,并且仅在没有字符是元音时才返回单词不变。

首先删除else; 当您看到不是元音的单个字符时,您不想返回,因为后面的字符可能是元音。现在,由于returnifword. 例如:

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
于 2013-02-26T17:44:46.013 回答
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 需要一个列表,而您正在向它传递一个字符串。我还注意到 translate_text 设置为空字符串。您需要在该列表中添加一些内容。

于 2013-02-26T17:46:28.017 回答
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()从 around中删除pig_latin_words。我也认为你想用空格而不是空字符串加入它们:" ".join()

于 2013-02-26T17:49:40.010 回答