Python def vowelCount()创建一个字典

时间:2013-12-09 23:45:47

标签: python dictionary function

我必须定义一个函数vowelCount()。输入是一个单词列表,我必须返回一个返回3个键的字典。它们是“更多的辅音”,其中包含的元音比元音更多,“更多的元音”具有更多的元音和“半元音”,两者的数量相等。

到目前为止,这是我的代码:

def voewlCount(wordList):
    myDict = {}
    vowelList = 'AEIOUaeiou'
    contents = wordList.split()
    for word in wordsList:
        if vowelList in wordList == word:
           myDict.append('half vowels')
        elif vowelList in wordList > word:
        myDict.append('more vowels')
    else:
        myDict.append('mostly consasants')

我在运行shell时收到错误消息,说这是一个属性错误,声明dict没有属性'append'

我更正了我的代码,但我仍然遇到问题......这是我的新代码,谢谢你的帮助

def vowelContent(wordList):
myDict = {'more consonants':[],'more vowels':[],'half vowels':[]}
vowels = 'aeiouAEIOU'
for word in wordList:
    if vowels in wordList < word:
        myDict['more consonants'].append(word)
    elif vowels in wordLists > word:
        myDict['more vowels'].append(word)
    else:
        myDict['half vowels'].append(word)
return myDict

say = ['do', 'you','know','the','definition','of','insanity','or','being','insane'] print(vowelContent(say))

当我打印该功能时,上面列表中的所有单词都会被放入'more consonants'

1 个答案:

答案 0 :(得分:2)

这是一些帮助您入门的框架。你可以填写我遗漏的逻辑。

def helper(word):
  """returns the number of vowels and consonants in the word, respectively"""
  # you fill this in
  return n_vowels, n_consonants

def voewlCount(wordList):  #sic
  result = {'more consonants': [], 'more vowels': [], 'half vowels': []}
  for word in wordList:
    nv, nc = helper(word)
    if #something:
      result['more consonants'].append(word)
    elif #something_else:
      result['more vowels'].append(word)
    elif #the other thing:
      result['half vowels'].append(word)
    else:
      # well this can never happen (or can it)?
  return result