anti_vowel - code works except for "e"


#1

def anti_vowel(text):
new=""
for i in text:
if i in "aeiouAEIOU":
new = text.replace(i, "")
return new

def anti_vowel(text):
new=""
for i in text:
if i not in "aeiouAEIOU":
new = new + i
return new

The second code works. But with the first one, it returns an error. Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hey lk Wrds!" when it should return "Hy lk Wrds!".

The letter "e" in "Hey" is not getting removed. But why. It is just like other vowels in the text :aeiousAEIOU".

But the second code works fine without any error.


#2

Hi,

Let's try to analyze your code 1:

You define a variable as blank (""), then you iterate through each letter.
If the current letter is a vocal, replace the current vocal and set it to the variable "new".
Then return the last result of the the assignment to new.

Let start the program with "Hey look Words!":

(I'll skip the non-vocal letters) "e" is in the vocal list, so set new equal to the sentence without any " e "'s.
-> "H e y" <-
So filter all " e "'s:
new = "Hy look Words!"

Next time the if is true (same result for both o's in look):
->l oo k
" o " is in vocal list, so new gets a new assignment
So filter all " o "'s only:
new = "Hey lk Wrds!"
-> You overwrite the old "new" variable with a new one

Next time the if is true:
-> W o rds! <-
" o " is in the vocal list, so new assignment to "new":
All " o "' are filtered anew:
new = "Hey lk Wrds!"

So the last result of new was "Hey lk Wrds!", and this will return only this one. :smiley:


#3

Here is a way to make it work:

def anti_vowel(text):
    for i in text:        
        if i in "aeiouAEIOU":            
            text = text.replace(i, "")    
    return text

#4

I tried this code but didn't keep it as it was instructed not to change the original list. So had to create a new list.

I should have tried with few of my own inputs and I could've realized the silly analysis. Thanks for the clarification.

I am new to coding itself and Python only interested me to get into coding.

It is really exciting to see how guys like you take efforts to help each other. I doubt if I would do that..:slightly_smiling:


#5

Yeah I'm sorry, didn't read the instructions this time, just tried to make your code word :pensive:

But there is also another way, but I bet you will find it yourself :smiley:

I do help to improve my debugging skills, because I couldn't code any better a few month ago ^^