Python:我的代码没有打印出元音的数量

标签 python

 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个参数(stringsentence),并且在调用函数时您只提供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/

相关文章:

python - 如何检查pandas中两个日期时间之间的日期时间

Python:为什么我的程序无法从列表写入文件?

带有整数的python集将大于10的整数分成两位数

python - 一热编码字符

python - 在同一行制作 ASCII 艺术打印

python - 如何自动点击 "Content"按钮

python - 这个 Django 正则表达式是什么意思? `?P`

python - 属于某个类的函数对象在属性查找时是否会受到特殊处理?

python - python中的函数装饰器是什么

python - 将列表映射到字典