PYTHON ONLY PYTHON ONLY Write a function called count_vowels() using what we lea
ID: 3852086 • Letter: P
Question
PYTHON ONLY
PYTHON ONLY
Write a function called count_vowels() using what we learned this week about list and recursive function. Here are the requirements:
1. solution uses recursive function. A recursive function has exit condition, and statements that calls itself.
2. function count_vowels takes a parameter of a String type: count_vowels(mystr)
3. function count_vowels returns a integer of how many vowels there are in the string parameter
4. a vowel is any letter in a, e, i, o, u, A, E, I, O, U
5. function count_vowels collects all the vowels in the string parameter, and prints out all the vowels at the end.
6. here's an example of calling the function and the output:
>>> count = count_vowels("banana")
aaa
>>> count
3
Explanation / Answer
#feel free to comment,,if u have doubt
my_list = []
def count_vowels(s):
count = 0
vowels = "aeiouAEIOU"
if not s:
return 0
elif s[0] in vowels:
my_list.append(s[0])
return 1 + count_vowels(s[1:])
else:
return 0 + count_vowels(s[1:])
return count_vowels(s[1:])
s = input('Enter a string : ')
count = count_vowels(s)
str = ''.join(map(str, my_list))
print("Vowels in given string : ",str)
print("Number of vowels: ", count)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.