gpt4 book ai didi

Python def vowelCount() 创建字典

转载 作者:行者123 更新时间:2023-11-28 17:44:12 25 4
gpt4 key购买 nike

我必须定义一个函数 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 时收到错误消息,说这是一个属性错误,指出字典没有属性 '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']
打印(vowelContent(说))

当我打印函数时,上面列表中的所有单词都被放入 '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 def vowelCount() 创建字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20483223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com