Reputation: 11
import random
count=0
candidateWords = ['HELLO', 'GOODBYE', 'NAME', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'BIG', 'SMALL', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'PIGEON', 'POSTER', 'TELEVISION', 'SPY', 'RIPPLE', 'SUBSTANTIAL', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
def countVowels(candidateWords):
vowel=['A','E','I','O','U']
for index in range (len(candidateWords)):
if vowel in candidateWords[0]:
count=count+1
return count
print(count)
else:
return False
When I trying to execute this code part nothing is displayed actually I needed to get count of value of "HELLO" word
Upvotes: 0
Views: 59
Reputation: 232
the main reason nothing is displayed is because you havent called the function after defining it. there are other mistakes in there too.
here is the correct code to print the vowel count for all the words in the word list:
candidateWords = ['HELLO', 'GOODBYE', 'NAME', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'BIG', 'SMALL', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'PIGEON', 'POSTER', 'TELEVISION', 'SPY', 'RIPPLE', 'SUBSTANTIAL', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
vowel=['A','E','I','O','U']
def countVowels(candidateWords):
for index in range(len(candidateWords)):
count=0
for _ in candidateWords[index]:
if _.upper() in vowel:
count += 1
print(candidateWords[index], 'has', count, 'vowels')
countVowels(candidateWords)
Upvotes: 1
Reputation: 106881
You can use the sum
function with a generator expression that iterates through each letter of each word in the list and tests if the letter is a vowel:
def countVowels(candidateWords):
return sum(letter in 'AEIOU' for word in candidateWords for letter in word)
Upvotes: 0