我必須定義一個函數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'
鍵
字典就像一個鍵/值存儲。你不會追加到字典。 要添加一個項目到字典,你寫這樣的事情: myDict ['key'] = value – Rami