Unexpected error in Anti Vowel


#1




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


def anti_vowel(text):
    for i in text:
        if i in "aeiouAEIOU":
                 text = text.replace(text[text.index(i)], "")
        print text
    return text
anti_vowel("Hey look Words!")

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

Above is my code and although it works fine it does not pass the compilers tests. Following is the error that the compiler throws:


Oops, try again. Your code looks a bit off--it threw a "substring not found" error. Check the Hint if you need help!


I would really appreciate your help. Stuck here since sometime now. Thanks in advance.


#2

We already have i in hand so why not just pass i ?
Make sure your indentation is correct.

also the error is due the fact that you're looping over your main string and when it finds some vowel ,it replaces all of them with an empty string!! about the error ,It says substring not found ?
means there is no character so you have error rather than index of it.

One more thing..
Instead looping over text and replacing the vowels with "", we can't direct loop over "aeiouAEIOU" ?


#3

hey greentreeme,
Iam not quite sure what you mean by "it replaces all of them with an empty string!! ", I have attached a photo, in o/p console I do achieve the intended result. Also if you'd be so kind to explain how looping over "aeiouAEIOU" works.


#4

take an example..

x = "I'm green."
x = x.replace("e","")

print x #prints "I'm grn."

You can see replace replaced all "e"'s with empty string ""


#5

yes that's correct but isn't that what is intended?


#6

text[text.index(i)]
Also above was done to find the index of i, is that not the correct method?


#7

what're you doing is to put the value then find the index using .index() method then put the index using [] operator to find the value again? (This makes us to get dependent on main string text)
rather this we can use i? as i is already that character(vowel)


#8

doesn't replace work only with index?


#9

In the example above, I have not used index() method or index but item?


#10

Yes thank you so much greentreeme! I made the change of passing i itself and it worked fine. thank you for being so patient with me. I am usually not so ditzy. Again your help is greatly appreciated, so is your patience.


#11

It's all good ! :slight_smile:
try this tool to visualize your first code, You will know what actually happening at each step and why the first method did not worked out!


#12

thank you, the tool is very convenient.


#13

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