擅长:python、mysql、java
<p>我认为这将有助于您(我已经注释了代码中没有的新行):</p>
<pre><code>def count_vowels(string):
sum_of_vowels = 0 # new line
vowel = 'aeiou'
for i in list(vowel):
if i in list(string):
sum_of_vowels += string.count(i) # new line
return sum_of_vowels
print(count_vowels('abcod'))
print(count_vowels('coliioor'))
print(count_vowels('colour'))
</code></pre>
<p>输出:</p>
<pre><code>2
5
3
</code></pre>
<p><strong>更好的方法:</strong></p>
<p>像这样做<a href="https://stackoverflow.com/questions/15489612/counting-occurrences-of-multiple-strings-in-another-string">this question</a>:</p>
<pre><code>def count_vowels(s):
return sum(vo in 'aeiou' for vo in s)
</code></pre>