我必须定义一个函数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