2013-12-09 103 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