Anti-Vowel


#1

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096#

Oops, try again. Does your anti_vowel function take exactly one argument (a string)? Your code threw a "string indices must be integers, not str" error.

Attempting to finish this anti-vowel exercise on my own but can't figure it out, I may be extremely wrong but any help at all would be much appreciated.

def anti_vowel(text):
    newText = []
    vowels = "aeiouAEIOU"
    
    for i in text:
        if text[i] not in vowels:
            newText.append(text[i])
    
    return newText

#2

Point1.

for i in text:
        if text[i] not in vowels:
            newText.append(text[i])

When you loop over text using i ,now i stores single character at each looping(as it iterates over string text).
As strings are accessed using integers but you're passing a single character string so it throws an error.

"string indices must be integers, not str"
You actually don't need to do it,as i itself is a single string character.
You need to make changes in last two line of that code that I mentioned.

Point2.
After you fix point1.

You're using list newtext to store all your characters after eliminating the vowels from text.

in this line of code..
return newText

It means you're returning list but you had to return string.
to do this use join method of python


#3

Genius, thank you very much. Solved


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.