python - 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 无论如何
解决方案
试试这样:
def findVowels(txt):
return sum(1 for t in txt if t in 'aeiouy')
推荐阅读
- ios - 如何在 Swift 中的 UITextField 上应用手机号码模式
- google-places-api - 我想澄清一下是否有可能将评论数量从每个位置 5 增加到所有位置
- c# - 如何在 Electron.NET 中创建子模式窗口?
- samsung-mobile - 如何使用 Bixby 在 Android 活动中的视图上生成“onClick()”事件
- c++ - 专业化功能的问题
- sql - SQL DATEDIFF 返回错误的结果
- javascript - 将 php 全局变量传递给 ajax.success()
- php - 如何在使用符号查询后回显当前 url?
- javascript - 如何将参数从Vue根传递到组件?
- node.js - sh:SET:在`npm start`中找不到命令