8.Anti-Vowel Code returning error


def anti_vowel(text):
print text
for i in text:
print i
if i in 'AaEeIiOoUu':
text= ''.join(text)

return text

Indentation has been taken care of. This is the error message I get-

Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hy lk Words!" when it should return "Hy lk Wrds!"

I really can't wrap my head around why it fails to work for the 'o' in Words! when it does for the other parts of the phrase. Any help would be appreciated.


I think the list.remove() function only removes the first match it finds. E.g.

["A", "B", "C", "A"].remove("A") # results in ["B", "C", "A"]


Hello )
This works for me
def anti_vowel(bcd):
for m in bcd:
if m in “aeiouAEIOU”:
bcd = bcd.replace(m,"’’)
return bcd


def anti_vowel(text):
new = “”
for c in text:
if c in “aeiouAEIOU”:
new = text.replace(c, “”)
return new

It doesn’t work for me…

It returns " Hey lk WRDS ! " when I should return " Hy lk WRDS ."

Why this doesn’t find ‘e’???


I did the same test using your code, and it works!!

For some motive, my code does not works because of the variable ‘new’. (A auxilliar variable)…


My code looks like this:
def anti_vowel(text):
letter = [x for x in text if x not in “aeiouAEIOU”]
#print ‘"’ + ‘’.join(letter) + ‘"’
return ‘’.join(letter)

anti_vowel(“Hey look Words!”)
And it works.


The problem is here:
new = text.replace(c, "")

You don’t change the text variable doing this. So when you replace “a” with “”, the next time you replace a vowel “a” will still be in text.

text = hello
new = text.replace("e", "")
# new is now "hllo"
new = text.replace("l", "")
# new is now "heo"

Look how @vera_zen did it:
bcd = bcd.replace(m,"'')


Oh! Thanks!!
I had not realized. :frowning:


I did it slightly differently. I started with an empty string, and concatenated each letter in turn to the string, so long as it wasn’t a vowel. Like this:

def anti_vowel(text):
    output = ""
    for letter in text:
        if letter in "aeiouAEIOU":
        elif letter == " ":
            output = output + letter
            output = output + letter
    return output

print anti_vowel("Hey You!")

def anti_vowel(text):
    return str(text.translate(None, "AEIOUaeiou"))


Wow :slight_smile: so translate is used like a replace. Thanks for sharing your solution :slight_smile:


Thanks for sharing your solution :slight_smile:


I used this though a little complicated :slight_smile:

def anti_vowel(text):
for let in text:
ca = 0
for c in “aeiouAEIOU”:

        if let == c:
            ca +=1
    if ca == 0:
return av


Daft question but what is the continue command and why is it necessary?


continue statements are a way of returning control to the start of the loop, without exiting them entirely (like break statements). In the example above, the loop is returned to the start whenever a vowel is encountered. As there is no other code contained within that if statement, the effect is to “skip over” every instance where a vowel is identified, and move the loop to the next letter in the string.

So the whole loop consists of three steps:

  1. If the letter is a vowel, do nothing and go back to the start of the loop.
  2. If the letter is a space (" "), add it to the new string.
  3. Otherwise, if the letter is not a vowel, and it’s not a space, just add it to the new string too.

If we were to replace continue with break, the loop would instead exit after the first vowel is encountered, instead of returning to the start of the loop. Try it out by putting different strings in the last print statement!



even shorter:

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