Matt D
Matt D

Reputation: 81

Python: Counting Vowels from List

I'm new to python and having some difficulty troubleshooting my script.

My assignment is to create some function that accepts a list of strings and returns the number of vowels within the entire list.

The game plan I'm attempting to follow is:

  1. Merge list elements into a single string
  2. Create a loop that tests if a string element is a vowel
  3. Use a counter variable to keep track of vowels in string
  4. Print the value of the counter variable when finished with loop

My code is not elegant, but it also does not work.

def vowelCounter(listName):
    new = ''.join(listName)
    n = len(new)
    count = 0
    vowels = 'aeiouAEIOU'
    i = 0
    for i in range(0,n):
        while i < n:
            if new[i] in vowels:
                count += 1
                i += 1
                return
            print count
            return
        return
    return

print(vowelCounter(["terrapin","station","13points"]))

Please forgive any stupid errors I may have. I will certainly appreciate any help you can offer!

Upvotes: 1

Views: 16731

Answers (4)

Will
Will

Reputation: 24709

So first, we have some general syntax problems.

  1. return exits from the function immediately; it doesn't just "end the loop".
  2. There's no point in initializing i to 0 above the loop. The for loop itself will just automatically set i to the current value in the list that range() returns.
  3. The while i < n is unnecessary; there's no need to loop through the string again for each character in the list.
  4. There is no need to manually increment i; for will do this for you automatically.
  5. You're printing the value inside the function, but you're also trying to print the return value of the function (but it doesn't return anything!).

So, if we fixed those issues, we'd have something like this:

def vowelCounter(listName):
    vowels = 'aeiouAEIOU'
    new = ''.join(listName)
    count = 0

    for i in range(0, len(new)):
        if new[i] in vowels:
            count += 1

    return count

But Python also allows the for loop to just iterate through each character of a string, so we don't need range() and len() at all:

def vowelCounter(listName):
    vowels = 'aeiouAEIOU'
    count = 0

    for char in ''.join(listName):
        if char in vowels:
            count += 1

    return count

But we can make this even awesomer, with List Comprehensions and the sum() function!

def vowelCounter(listName):
    vowels = 'aeiouAEIOU'
    count = sum([1 for char in ''.join(listName) if char in vowels])
    return count

What we basically do here, is to make a list of 1s for each letter that is a vowel (and if it's not a vowel, we don't put anything in our new list). Then we use sum() to add up all of the numbers (1's) in the list, which is our total number of vowels.

Or we could even make this a one-liner:

def vowelCounter(listName):
    return sum([1 for char in ''.join(listName) if char in 'aeiouAEIOU'])

Upvotes: 3

Pratanu Mandal
Pratanu Mandal

Reputation: 647

The step-by-step logic you supplied is correct.
However, the code you posted does not follow the logic and is incorrect.

Try the following code instead:

def vowelCounter(listName):
    string = ''.join(listName)
    count = 0
    vowels = 'aeiouAEIOU'
    for ch in string:
        if ch in vowels:
            count += 1
    return count

print(vowelCounter(["terrapin","station","13points"]))

Upvotes: 2

Saqib Ali
Saqib Ali

Reputation: 12585

>>> import re
>>> vowels = re.compile('[AEIOU]', re.IGNORECASE)
>>>
>>> def vowelCounter(listName):
...     return len(vowels.split("".join(listName)))-1
...
>>> vowelCounter(["terrapin","station","13points"])
8
>>> vowelCounter(["terrapin","station","13pOInts"])
8

Upvotes: -1

6502
6502

Reputation: 114559

The code is mostly ok... however

  • a for i in ... automatically increments i, so i += 1 is not needed
  • the for does the looping... no need to put another while loop inside it
  • return quits the function and you should only use it at the very end to give back the result that has been computed with return count

Upvotes: 1

Related Questions