2013-12-09 195 views
3

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

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

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,称这是一个属性错误sating一个dict有没有属性“追加”

我纠正我的代码,但我仍然有问题...这是我的新代码,谢谢你的帮助

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

字典就像一个键/值存储。你不会追加到字典。 要添加一个项目到字典,你写这样的事情: myDict ['key'] = value – Rami

回答

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 
+0

在单词中是否有其他元音(例如,标点符号)?助手可以是一个类似于cmp的函数,它比较单词中的元音和辅音的数量(我们不需要他们的计数,只需要比较就可以了) – jfs