1

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!

Merlin
  • 24,552
  • 41
  • 131
  • 206
Matt D
  • 81
  • 1
  • 2
  • 11

4 Answers4

3

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'])
Will
  • 24,082
  • 14
  • 97
  • 108
  • 1
    Talk about elegance... Thanks for the great explanation! – Matt D Feb 13 '16 at 07:28
  • No problem, thanks, glad to help! Python is very flexible and expressive, and there's always a few correct approaches, but the goal is usually to find one that's the right combination of simple, efficient, but also readable. You we're definitely on the right track though! – Will Feb 13 '16 at 09:00
  • 1
    @MattD If you think that this answer was good and sufficient, and correct (of course), you should go ahead and accept it, :) – Tacocat Feb 16 '16 at 05:36
2

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"]))
Pratanu Mandal
  • 597
  • 8
  • 23
  • Is print necessary to display the value that the function returns? – Matt D Feb 13 '16 at 07:23
  • Yes, the function returns the vowel count. So print is necessary to display the value returned from the function. – Pratanu Mandal Feb 13 '16 at 07:37
  • However, you may print the vowel count directly from inside the function by replacing the last line `return count` with the following two lines `print count` and `return` – Pratanu Mandal Feb 13 '16 at 07:37
1

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
6502
  • 112,025
  • 15
  • 165
  • 265
-1
>>> 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
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272