我有一个问题,我必须用 Python 解决一个任务,但我不知道该怎么做。任务是定义一个函数 number_of_vowels,其中输出应该是单词中元音的数量。使用此函数,我必须编写另一个,many_vowels,它与一个列表一起工作,一个数字,其中数字表示一个单词中必须至少有多少个元音才能附加到结果列表中,然后我必须附加这个单词。感谢所有帮助我的人;D。
这是代码:
Wort = "parameter"
def number_of_vowels(Word):
result = 0
counter0 = 0
while result < 20:
if Word[counter0] == 'a' or 'e' or 'i' or 'o' or 'u':
result = result + 1
counter0 = counter0 + 1
else:
counter0 = counter0 + 1
return result
Words = []
counter1 = 0
def many_vowels(List , number):
if number_of_vowels(List[counter1]) < number:
counter1 + 1
else:
Words.append(List[counter1])
counter1 + 1
return Words
这段代码只给了我字母 a 的答案,而不是其他元音的答案。为了
print(number_of_vowels(Wort))
输出是:1,但是这个词中有 4 个元音,它还说:第 21 行,在 many_vowels IndexError: string index out of range