8. anti_vowel


#1



8. anti_vowel


My code gave the following error message... 'Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "['']" when it should return "Hy lk Wrds!".'


Does anybody know what i've done wrong or how to fix this code using this method?
Thanks guys!


def anti_vowel(text):
    myList = []
    for char in text:
        vowelPlace = text.index(char)
        if char == 'a' or 'e' or 'i' or 'o' or 'u' or 'A' or 'E' or 'I' or 'O' or 'U':
            myList.append('')
            return myList
            print myList
        else:
            myList.append(char)
            return myList
            print myList


#2

i think this condition won't work , this verify just if char equal to "a" ( so you need to repeat char =='e' or char == 'i' ...)

also you don't have to return the list every time in for loop , you should return the list after


#3

try to use:
import string
text.string.replace("a","")

it can replace "a" with empty


#4

To answer your question:

1)
You should not return in if-else statements, since you function will return immediately once in either IF or ElSE.

2)

 if char == 'a' or 'e' or 'i' or 'o' or 'u' or 'A' or 'E' or 'I' or 'O' or 'U':

should be

if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u' or char == 'A' or char == 'E' or char == 'I' or char == 'O' or  char == 'U':

which is really long haha

3) Lastly, you should return text not a list.

Here's my solution:

Since you know there are only 5 vowels, why not store them in a list, rather than a long condition for if-else statment.

def anti_vowel(text):
    vowels = ["a","e","i","o","u"]
    t = ""
    for c in text:
        if c.lower() not in vowels:
            t += c
    return t

#5

@methodninja87513 @azazaz
Thanks very much - your solution was really interesting for me, I'm going to try and do some things along those lines in future.
However, I quite like fixing things... I tried your tips although it still didn't work (code below)

> def anti_vowel(text):
>     phrase = text
>     for char in text:
>         if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u' or char == 'A' or char == 'E' or char == 'I' or char == 'O' or char == 'U':
>             text.replace(char,"")
>     return text
>     print text

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


#7

1) you should return object phrase since you are trying to manipulate it
2) .replace() method returns a string, it does not change the calling object
3) you are looping object text, its best not to manipulate it while looping it.

so something like this would work for your design

def anti_vowel(text):
    phrase = text
    for char in text:
        if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u' or char == 'A' or char == 'E' or char == 'I' or char == 'O' or char == 'U':
            phrase = phrase.replace(char,"")
    return phrase

#8

Thank you!!! That worked (also thanks for explaining it). Great to see so many helpful people in this community when you're a bit of a noob like me.


#9

this is quite clean way of writing it:

vowels = "aeiouAEIOU"

def anti_vowel(text):
phrase = text
for char in text:
if char in vowels:
phrase = phrase.replace(char,"")
return phrase


#10

I wrote this code, it works fine but I still get this oops message "Oops,try again. Your function fails on anti_vowel("Hey look Words!").It returns "Hy lk Wrds!" when it should return "Hy lk Wrds!"
I don't get why ?!
def anti_vowel(text):
result =" "
for c in text:
if c=="a" or c=="A" or c=="e" or c=="E" or c=="i" or c=="o" or c=="O" or c=="u" or c=="U":
continue
else:
result= result+c
return result


#11

Yes, i aggree with your script cody. This is my script just gives a different way to define the vowels.

def anti_vowel(text):
    vowel = 'aiueo'
    anti_vowel = ''
    for char in text:
        if char.lower() not in vowel:
            anti_vowel = anti_vowel + char
    return anti_vowel

#12

Hey everybody,
I redid all of the "challenges" and here is a simple solution for the anti-vowel.
As you probably know, there are two alternatives for writing the for loop in the function.

First Syntax:

def anti_vowel(text):
result = ""
for char in range(len(text)):
if text[char] not in "aeiouAEIOU":
result+=text[char]
return result

Second Syntax:

def anti_vowel(text):
result = ""
for char in text:
if char not in "aeiouAEIOU":
result+=char
return result

print anti_vowel("Hello World")


#13

:slight_smile:cool and simple ! :smiley:


#14

Hi, there! This one took me a bit but try this:
VOWELS = set("aeiouAEIOU")

def anti_vowel(text):
result = text
for c in text:
if c in VOWELS:
result = result.replace(c, "")
return result

anti_vowel("Hey look Words!")


#15

Remember This, any code that comes after return never executes.

   Place print before return