def is_vowel (character):
if (character == 'a'or character == 'o' or character == 'u' or character == 'i' or character == 'e'):
return True
else:
return False
def count_vowels_iter(string, sentence):
count = 0
for character in sentence:
if (is_vowel(character) == True):
count==count+1
return count
count = count_vowels_iter('sentence')
print (count)
有人可以帮助我吗,我是新手。一定有什么东西我没有找到。
最佳答案
首先,count_vowels_iter
需要2个参数(string
和sentence
),并且在调用函数时您只提供1个。
其次。您的 return
语句需要位于 for
循环完成之后,如下所示:
def count_vowels_iter(string, sentence):
count = 0
for character in sentence:
if (is_vowel(character) == True):
count=count+1
return count
count==count+1
应该是 count=count+1
因为您正在分配 count
一个新值,而不是比较它。
关于Python:我的代码没有打印出元音的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69688845/