【问题标题】:(Python) If Else issue and list to string conversion issue(Python) If Else 问题和列表到字符串转换问题
【发布时间】: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


【解决方案1】:

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

首先删除else;当您看到不是元音的单个字符时,您不想返回,因为后面的字符可能是元音。现在,由于您的 if 中有一个 return 语句,您知道如果您到达循环的末尾而不返回 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

【讨论】:

    【解决方案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
    

    正如其他地方提到的,您还可以从pig_latin_words 周围删除str()。我还认为您想用空格而不是空字符串加入它们:" ".join()

    【讨论】:

      【解决方案3】:

      第一期:

      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 设置为空字符串。您需要在该列表中添加一些内容。

      【讨论】:

      • 这并不能解决第二个/第三个问题,因为pig_latin_words 在传递给translated_text 时是一个空列表,与此相比没有任何变化。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 2016-03-18
      • 1970-01-01
      相关资源
      最近更新 更多