Pallu08
Pallu08

Reputation: 25

How to iterate through multiple items using split() and check for vowels using for loops?

I have the following homework problem:

Write a function countVowels() that takes a string as a parameter and prints the number of occurrences of vowels in the string.

>>> countVowels('It was the best of times; it was the worst of times.')
a, e, i, o, and u appear, respectively, 2, 5, 4, 3, 0 times.
>>> countVowels('All for one, and one for all!')
a, e, i, o, and u appear, respectively, 3, 2, 0, 4, 0 times.
>>>

I've created a function that with the intention of taking a string like str1 = "bananas are tasty", splitting the string and assigning it to a variable so it becomes str1_array = ['bananas', 'are', 'tasty'], then using for loops to iterate over str1_array, count the vowels a, e, i, o, and u respectively, and include the amount of each vowels in the ending print() statement.

I've run into a problem where the function will only count for "a" in the first item in str1_array, and it will repeat that amount in my ending print statement.

i.e for str1_array = ['bananas', 'are', 'tasty'] I get the print statement A, E, I, O, and U appear, respectively, 3 3 3 3 and 3 times.

def countVowels(str1):
    str1_array = str1.split(" ")
    vA = ["A", "a"]
    vE = ["E", "e"]
    vI = ["I", "i"]
    vO = ["O", "o"]
    vU = ["U", "u"]

    vA_count = 0
    for vA in str1_array:
        vA_count = vA_count + 1
    vE_count = 0
    for vE in str1_array:
        vE_count = vE_count + 1
    vI_count = 0
    for vI in str1_array:
        vI_count = vI_count + 1    
    vO_count = 0
    for vO in str1_array:
        vO_count = vO_count + 1    
    vU_count = 0
    for vU in str1_array:
        vU_count = vU_count + 1

    print("A, E, I, O, and U  appear, respectively, ", vA_count, vE_count, vI_count, vO_count, "and", vU_count, "times.")

Upvotes: 0

Views: 188

Answers (2)

user2891462
user2891462

Reputation: 3343

Take a look at str.count(). You can simply do:

def countVowels(s):
    vowels = ['a', 'e', 'i', 'o', 'u']

    # Save the string in lower case to also match upper case instances
    lower_s = s.lower()

    # Possible improvement: generate this message from vowels list
    msg = 'a, e, i, o, and u appear, respectively'

    for v in vowels:
        msg += ', ' + str(lower_s.count(v))

    msg += ' times.'

    print(msg)

This solution is, admittedly, not great, since it iterates over the whole string once per vowel. You could maybe improve it by iterating over the string just once and counting how many time any character (not only vowels) shows up. You can then simply print the values you are interested in:

from collections import defaultdict

def countVowels(s):
    vowels = ['a', 'e', 'i', 'o', 'u']
    lower_s = s.lower()

    # Create a dictionary of int values to store the number of appearances of a
    # letter
    results = defaultdict(int)

    for c in lower_s:
        results[c] += 1

    msg = 'a, e, i, o, and u appear, respectively'

    for v in vowels:
        msg += ', ' + str(results[v])
    msg += ' times.'

    print(msg)

Upvotes: 2

Mathieu
Mathieu

Reputation: 5756

Let's first spot problems in your code:

First, you split the stirng: str1_array = ['bananas', 'are', 'tasty']. Then you define your vowels as e.g. vA = ["A", "a"]. And then, you are looping to count with:

vA_count = 0
for vA in str1_array:
    vA_count = vA_count + 1

Actually, that last part doesn't do that at all. If you do

for vA in str1_array:
    print (vA)

You will get:

'bananas'
'are'
'tasty'

What happens in that the variable vA previously defined as ["A", "a"] is overwritten and define successively as the 3 words in str1_array.

Aside from this, counters method are already implemented, there is no need to reprogram those. For instance, you could do:

from collections import Counter
c = Counter("bananas are tasy".lower())

This will lower the string, meaning the upper case letters get turned into lower case letters; and then it will create a counter object. Then you can access the number of vowels as:

IN: c['a']
OUT: 5

And thus with a loop:

vowels = ['a', 'e', 'i', 'o', 'u']
for v in vowels:
    print c[v]

Upvotes: 1

Related Questions