【发布时间】:2020-10-02 15:07:49
【问题描述】:
您好,有没有更好的方法来编写此代码,而不是编写重复的多个 if 语句?我正在尝试找到一种更好的方法来编写此代码。基本上我想计算给定字符串 s 中匹配字母的总数。
s = 'abcdbobbobbegkhl'
count = 0
for letter in s:
if letter == 'a':
count += 1
if letter == 'e':
count += 1
if letter == 'i':
count += 1
if letter == 'o':
count += 1
if letter == 'u':
count += 1
print('Number of vowels: ' + str(count))
【问题讨论】:
标签: python