Anti_vowel


#1



anti_vowel

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

can anyone help me understand what i am doing wrong and give me the correction and what the computer interprets it as


consonants=[]
def anti_vowel(text):
    vowels="AEIOUaeiou"
    for i in vowels:
        for c in text:
            if c != i:
                consonants.append(c)
                return consonants


#2

consonants=[]
def anti_vowel(text):
    vowels = list("AEIOUaeiou") #A for/in loop only works on lists, this splits it into one, each entry 1 character long
    text = list(text) #See above
    for c in text:
    	truey = 'true' # You need this because of how the loops are ordered
    	for i in vowels:
            if c == i:
                truey = 'false' # You had the loops the wrong way, so, if the character didn't match the first vowel, it appended to the consonants list
    	if truey == 'true':
        	consonants.append(c)
    return consonants #Your return statement was returning as soon as it found a character that worked

#3

This work fine:

def anti_vowel(text):
a=""
for i in text:
if i !="a" and i!="e" and i!="i" and i!="o" and i!="u" and i!="A" and i!="E" and i!="I" and i!="O" and i!="U":
a=a+(i)
return a


#4

Thanks for the help:grinning:


#5

This variable should be defined inside the function so it is local.

Iterate only over the text, not the vowels. We need to find repeats.

    result = []
    for i in text:
        if i not in vowels:
            result.append(i)
    return ''.join(result)

#6

A post was split to a new topic: Can we use this code? answer is none


#8

works
def anti_vowel(test):

test1=[]
for char in test:
    if char != 'a' and char!='e' and char!='i' and char!='o' and char!='u' and char != 'A' and char!='E' and char!='I' and char!='O' and char!='U':
        test1.append(char)    
return ''.join(test1)

#9

Do you not find that to be overly repetitive and verbose? That code is a demonstration of how not to code, and while it may work, it proves nothing except refusal to use tools readily at hand.

Non-repetitive, concise code:

    result = []
    for i in text:
        if i not in "aeiouAEIOU":
            result.append(i)
    return ''.join(result)

We can further refactor the above into a single return statement:

def anti_vowel(x):
    return ''.join([i for i in x if i not in 'aeiouAEIOU'])

#10

This is the best one. Thanks


#11

Best example code in this thread, very nice.


#13

this is so nice! thank you


#16

Hi i was just checking your code and I realized your return indent was a little off. It should not be within your for loop otherwise it will print each letter with one other letter concatenated on a new line which throws a error. But other than that excellent code!


#17

Was waiting for someone to spot that. Good eye! Will fix it, now.


#18

def anti_vowel(text):
result = ""

for c in text:
    if c not in "aeiouAEIOU":
        result += c

return result

print anti_vowel("Hey You!")


#19

Like your code a lot. I used lambda/filter but it's probably less pythonic.


#20

This is a great code, I was struggling with the length and repetition of it all. I saw your condensed code and it helped me understand it.

Thanks!


#21

A post was split to a new topic: Is there a reason we define an empty string


#22