Question on Anti_vowel topic


#1

hello,
so i have been doing the Anti_vowel topic and i did it eventually, but my code is a bit long winded, as you can see.
What i wanted to know is how to shorten down the if, elif stetement because it was horrible to writeall those each time.

def anti_vowel(text):
    word = []
      for letter in text:
        word.append(letter)
        for i in word:
            if "a" in word:
                word.remove("a")
            elif "A" in word:
                word.remove("A")
            elif "e" in word:
                word.remove("e")
            elif "E" in word:
                word.remove("E")
            elif "i" in word:
                word.remove("i")
            elif "I" in word:
                word.remove("I")
            elif "o" in word:
                word.remove("o")
            elif "O" in word:
                word.remove("O")
            elif "u" in word:
                word.remove("u")
            elif "U" in word:
                word.remove("U")
    return "".join(word)

i tried including the lower and upper letters to cut down by half:
if "a" or "A" in word:
but this was giving me an error, it would have been nice but doesn't work.
i couldn't use the or/and within an in usage(who knows why?)
is there a shorter wayi can write this code for efficiency instead of all those elif's i have.

thank you in advance

Rob


Replace this line with your code.


#2

if you wanted to do this you would get:

if "a" in word or "A" in word:

still not very efficient.

You should be able to use in, in a more effective way

For a simply string (like Hey You!) your program makes 24 loop iterations. This number will increase if the string becomes longer

i personally wouldn't even use .remove() to solve this problem, given the complication its gives (index shifts), which you can overcome like you did by looping over the string many times

go for the opposite approach, append constants to a new list/string (you pick/choice)


#3

Hey stetim94
so i was going over the code and i found a quick way to do it, lot easier than my first.

def anti_vowel(text):
word = []
for letter in text:
if letter not in "aeiouAEIOU":
word.append(letter)
return "".join(word)

print anti_vowel("hello")
This prints hll

thanks for the assistance and guidence to make me look again.
much appreciated
Rob


#4

that is a much better solution :slight_smile: Well done :slight_smile:


#5