首页 > 解决方案 > Python函数将在给定字符串中查找和计算元音

问题描述

我正在尝试用 python 编写一个代码,它将查找并计算任何给定字符串中元音的数量。无论如何,我的函数都会打印出 0 个元音。有小费吗?

txt = input("Type text: ").lower()

def findVowels():
    global txt
    vowels = 0
    for letter in txt:
        if letter == 'aeiouy':
            vowels += 1
    return vowels


print(findVowels())

元音数 = 0 无论如何

标签: python

解决方案


试试这样:

def findVowels(txt):
    return sum(1 for t in txt if t in 'aeiouy')

推荐阅读