嗨,有没有更好的方法来编写此代码而不是编写重复的多个 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))
Run Code Online (Sandbox Code Playgroud) python ×1