juhnellove
juhnellove

Reputation: 15

Counting the vowels from a string

This is what I'm trying to do. I have some text and I'm trying to filter through it and only print out the TOP TEN words with the most vowels. This is only a portion of my code, but this is the only part that I keep getting an error on. The error I get is: unorderable types NoneType() < int()....and I get that error when i try and sort my words. (let me know if I am unclear or confusing) How can I fix this error (in my topTen function)? This is the little piece of code I'm talking about:

def topTen(text):
    words = textToWords(text)
    words.sort(key=countVowels, reverse=True)
    for ctr in range(10):
        print(words[ctr])

def textToWords(T):
    W = []

    for line in T:
        words = line.split()

        for word in words:
            W.append(word.lower())
    return W

def countVowels(st):
    ctr = 0
    for ch in st:
        if ch in "aeiou":
            ctr =+ 1
            return ctr

Upvotes: 1

Views: 125

Answers (1)

Paul Cornelius
Paul Cornelius

Reputation: 10946

The last line of countVowels is not indented correctly. When the if statement is taken the function will return 1; when the if statement is never taken (the word contains no vowels) the function returns None. Re-indent the return ctr so it's outside the loop and I think the program will work.

Upvotes: 1

Related Questions