• Register
Log In


for 'a' in string is not the proper way to look for cases of a in a string. The syntax for item in list needs item to be a variable, and you have a literal value instead, which is why you're obtaining that error. 

Apply Method 1:

for char in string:
    if char in "aeiou":
        num_vowels += 1

Method 2: Fix this

def count_vowels(string):
    num_vowels = 0
    vowels = ('a', 'e', 'i', 'o', 'u')
    for letter in string:
        if letter in vowels:
            num_vowels += 1

count_vowels("pear tree")
count_vowels("o a kak ushakov lil vo kashu kakao")
count_vowels("tk r n m kspkvgiw qkeby lkrpbk u thouonm fiqqb kxe...(This just goes on forever"))

Mrthod 3: 

for 'a' in string:

In this case, A for-loop assigns values from an iterator to its loop variable. You've attempted to offer the name 'a' to the loop variable, which is a literal string, not an identifier.

Hopefully, now you are able to solve this issue by following these steps.

posted Jun 29 in python 12,840 points