Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / study / engineering / computer science / questions and answers / in pytho

ID: 3696835 • Letter: H

Question

home / study / engineering / computer science / questions and answers / in python, write a recursive function, containsvowel, ... Question In Python, Write a recursive function, containsVowel, which accepts one parameter containing a string value and returns True if the string contains a vowel. A string contains a vowel if: The first character of the string is a vowel or The rest of the string (beyond the first character) contains a vowel

My code:

def containsVowel(s): if s[0:] == "aeiouAEIOU":

return True

else:

return False

it doesnt work however. It actually does the opposite of what i want it to do. Also it doesnt work for all variables.

Explanation / Answer

def containsVowel(str):
   if(len(str) ==1):
       if(str[0]=='a' or str[0]=='A' or str[0]=='e' or str[0]=='E' or str[0]=='i'
           or str[0]=='I' or str[0]=='o' or str[0]=='O' or str[0]=='u' or str[0]=='U'):
           return True
       else:
           return False
   else: # for recursive algo get the first element result and repeat it for remaining items
   return (str[0]=='a' or str[0]=='A' or str[0]=='e' or str[0]=='E' or str[0]=='i'
           or str[0]=='I' or str[0]=='o' or str[0]=='O' or str[0]=='u' or str[0]=='U') and containsVowel(str[1:])

print containsVowel("aeiou")
print containsVowel("aeion")
print containsVowel("aenou")
print containsVowel("I")