Python: My code is not printing out the count of vowels

Viewed 67
 def is_vowel (character):

   
    if (character == 'a'or character == 'o' or character == 'u' or character == 'i' or character == 'e'):
    
        return True
    else:
        return False

def count_vowels_iter(string, sentence):
    count = 0

    for character in sentence:
       if (is_vowel(character) == True):
        count==count+1
        return count
count = count_vowels_iter('sentence')
print (count)

Can someone please help me I am a newbie. There must be something that I am not finding.

2 Answers

First of all, count_vowels_iter, takes 2 arguments (string and sentence) and when calling the function you only provide 1.

Secondly. your return statement needs to be after the for loop has been completed as follows:

def count_vowels_iter(string, sentence):
    count = 0

    for character in sentence:
       if (is_vowel(character) == True):
        count=count+1
    return count

count==count+1 should be count=count+1 as your are assigning count a new value, not comparing it.

There are a few problems here. count_vowels_iter is accepting two parameters: string and sentence, but you are only passing in one. The next issue is the way you are incrementing count. There should only be one = sign. The last issue is the location of your return statement. You are returning the count once for every character in sentence. You should remove the indentation so that it only returns once after all characters have been checked.

Related