【问题标题】:Identify if a word in a list starts with a vowel [closed]确定列表中的单词是否以元音开头[关闭]
【发布时间】:2021-10-11 01:55:06
【问题描述】:

我需要确定这些单词是否以元音开头。

year= ["january","february","march","april","may","june","july","august","september","october","november","december"]
vowels = "aeiou"

for month in range(len(year)):
    if month.startswith(vowels):
         if_vowel = "Yes"
    else:
         if_vowel = "No"
print("Does the month of {0} start with a vowel? {1}".format(year[i],if_vowel)

理想情况下,它会根据月份是否以元音开头打印是或否

【问题讨论】:

  • 没有一个月份名称以“aeiouy”开头(这是您最初测试的)。
  • 这能回答你的问题吗? checking if the first letter of a word is a vowel
  • 嗨!请不要忘记在您的帖子中包含一个实际问题!我不清楚您实际上遇到了这个问题的哪一部分。解释器是否抛出异常(您没有预料到)?请包括那个!它是否返回了意外的输出?请包括显示的内容和您想要的内容!

标签: python for-loop if-statement


【解决方案1】:
for month in year:
    print(f"Does the month of {month} start with a vowel? {'Yes' if month[0] == 'e' else 'No'}")

您可以使用 Python3 的 f-String 来简化格式化。然后,在 f-String 中,您可以使用条件表达式,例如如果月份的第一个字母是“e”则返回“Yes”,否则返回“No”。

【讨论】:

    【解决方案2】:

    如果你迭代 year 而不是 indecies range(len(year)) 会更容易。然后你可以检查第一个字符month[0]是否是month[0] in vowels的值。

    year= ["january","february","march","april","may","june","july","august","september","october","november","december"]
    vowels = "aeiouy"
    
    for month in year:
        if month[0] in vowels:
             if_vowel = "Yes"
        else:
             if_vowel = "No"
        
        print("Does the month of {0} start with a vowel? {1}".format(month,if_vowel))
    

    【讨论】:

      【解决方案3】:

      您必须检查月份名称中的第一个字母是否以元音开头。或不。迭代索引将帮助您获得预期的输出。 'y' 不是元音。所以更新了你的代码。

      year= ["january","february","march","april","may","june","july","august","september","october","november","december"]
      vowels = "aeiou"
      
      for i in range(len(year)):
          if year[i][0] in vowels:
               if_vowel = "Yes"
          else:
               if_vowel = "No"
          print("Does the month of {0} start with a vowel? {1}".format(year[i],if_vowel))
      

      输出:

      Does the month of january start with a vowel? No
      Does the month of february start with a vowel? No
      Does the month of march start with a vowel? No
      Does the month of april start with a vowel? Yes
      Does the month of may start with a vowel? No
      Does the month of june start with a vowel? No
      Does the month of july start with a vowel? No
      Does the month of august start with a vowel? Yes
      Does the month of september start with a vowel? No
      Does the month of october start with a vowel? Yes
      Does the month of november start with a vowel? No
      Does the month of december start with a vowel? No
      

      【讨论】:

        【解决方案4】:
        year = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november",
                "december"]
        vowels = ("a", "o", "e", "i", "u", "y")
        
        for month in year:
            if month.startswith(vowels):
                if_vowel = "Yes"
            else:
                if_vowel = "No"
            print("Does the month of {0} start with a vowel? {1}".format(month, if_vowel))
        

        【讨论】:

          【解决方案5】:
          year = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november",
                  "december"]
          vowels = ("a", "o", "e", "i", "u", "y")
          
          for month in year:
              if month[0] in vowels:
                  if_vowel = "Yes"
              else:
                  if_vowel = "No"
              print("Does the month of {0} start with a vowel? {1}".format(month, if_vowel))
          

          【讨论】:

            猜你喜欢
            • 2012-10-01
            • 1970-01-01
            • 2020-07-13
            • 2014-03-14
            • 2020-10-25
            • 2021-07-30
            • 1970-01-01
            • 1970-01-01
            • 2015-12-16
            相关资源
            最近更新 更多