Anti_vowel doesn't change E


I tried to find a post with this question, I only found one but he didn't find the answer.

def anti_vowel(text):
    for x in text:
        if x in "aeoiuAEOIU":
            text2 = text.replace(x, '')
    return text2

It returns "our function fails on anti_vowel("Hey look Words!"). It returns "Hey lk Wrds!" when it should return "Hy lk Wrds!".
I don't get why. any help?


You're replacing one letter in text, so you're never replacing all of them, just ever one. (Note that you have two variables, text and text2)


Thanks, didn't realize it was only changing the 'o' . It was as simple as changing text2 to text, I thought that because the strings are immutable in python I wasn't able to do so.


They are. But the variables are not the strings, they just refer to strings.


This is my code, it seems to be working....

def anti_vowel(text):
new_text = ""
no_text = "aAeEiIoOuU"
for i in range (0,len(text),1):
for j in range (0,len(no_text),1):
if text[i] == no_text[j]:
new_text = new_text + text[i]
return new_text


This is simpler

vowels = ['a','e','i','o','u']

def anti_vowel(text):
final_str = ''
for i in text:
if i.lower() not in vowels:
final_str = final_str + i
return final_str


I decided to use an append approach

def anti_vowel(text):
txt = list(text)
new_text = []
vowels = "aeiouAEIOU"
for i in txt:
if i not in vowels:
new_text = ''.join(new_text)
return new_text

Think though it could have been done more gracefully.

@ulisesviado this is your code reworked a bit instead of using "x in" I used "x not in"
and set text2 = "" from the start. then just added to the text2 in the if statement

def anti_vowel(text):
    text2 = ""
    for x in text:
        if x not in "aeoiuAEOIU":
            text2 += x
    return text2


my solution:


could someone tell me why this code doesnt work. i'm trying to print out the same variable which is supposed to be the user's input. I know there are other ways of doing it, i just dont know why this way is not working.

Thanks in advance

`vowels = ['a','e','i','o','u','A','E','I','O','U']
text = raw_input('your sentence here: ')

def anti_vowel(text):

for x in text:
    if x == vowels[x]:
        text.replace(x, '')
    print text`

its returning this error:

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



I too used that initial approach and it worked perfectly + was easily readable and understandable, but I then decided to slim it down slightly and went with:

def anti_vowel(text):
    vowels = ['a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U']
    to_string = ""

    for each in text:
        if each not in vowels:
            to_string = to_string + each
    return to_string