Reputation: 81
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:
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
Reputation: 24709
So first, we have some general syntax problems.
return
exits from the function immediately; it doesn't just "end the loop".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.while i < n
is unnecessary; there's no need to loop through the string again for each character in the list.i
; for
will do this for you automatically.print
ing 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 1
s 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
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
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
Reputation: 114559
The code is mostly ok... however
for i in ...
automatically increments i
, so i += 1
is not neededfor
does the looping... no need to put another while
loop inside itreturn
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