擅长:python、mysql、java
<p>使用slice删除第一个字符并测试其他字符。您不需要else块,因为您需要为每种情况调用函数。如果把它放在else块中,那么当最后一个字符是元音时,它将不会被调用:</p>
<pre><code>### Improved Code
def recVowelCount(s):
'return the number of vowels in s using a recursive computation'
vowel_count = 0
# You should also declare your `vowels` string as class variable
vowels = "aEiou".lower()
if not s:
return 0
if s[0] in vowels:
return 1 + recVowelCount(s[1:])
return recVowelCount(s[1:])
# Invoke the function
print recVowelCount("rohit") # Prints 2
</code></pre>
<p>这将使用第一个字符切片的新字符串调用递归函数。</p>