我必须定义一个函数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'
键 最佳答案
这里有一些框架可以帮助你开始。你可以填写我遗漏的逻辑。
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
关于python - Python def vowelCount()创建字典,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20483223/