简体   繁体   中英

Error str object not callable in vowel counting program

Hi I am trying to write a program that counts the vowels in a sentence and then prints out the count of each vowel individually.

Sentence = input("Please enter sentence: ")
a = 0
e = 0
i = 0
o = 0
u = 0
for index in range(0, len(Sentence) - 1):
    if Sentence(index) == "a":
        a = a + 1
    elif Sentence(index) == "e":
        e = e + 1
    elif Sentence(index) == "i":
        i = i + 1
    elif Sentence(index) == "o":
        o = o + 1
    elif Sentence(index) == "u":
        u = u + 1
print(a, e, i, o, u)

Getting the following error :

Please enter sentence: Traceback (most recent call last): File "script.py", line 8, in if Sentence(index) == "a": TypeError: 'str' object is not callable Command exited with non-zero status 1

Any help would be be appreciated. Thanks

Looks like python to me.

for index in range(0, len(Sentence) - 1):
    if Sentence[index] == "a": #You have Sentence(index)
        a = a + 1
    elif Sentence[index] == "e":
        e = e + 1
    elif Sentence[index] == "i":
        i = i + 1
    elif Sentence[index] == "o":
        o = o + 1
    elif Sentence[index] == "u":
        u = u + 1
print(a, e, i, o, u)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM