Anti_vowel


#1

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

can anyone tell me what's wrong with this code?


#2

This is comparing a string to a tuple. Cannot be done (always False).

>>> vowels = ('a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U')
>>> 'word' == vowels
False
>>> 'a' == vowels
False
>>> "a" in vowels
True
>>>

#3

so i change the statement with
if text in vowels


#4

It is still a string of characters which will always yield false. Iterate through text and check each letter against the vowels tuple.

if x in vowels:

#5

ok cool,
now it is working.
Thanks man.


#6

Since you've come this far start using list comprehensions and other methods its efficent.


return "".join([text[i] for i in range(len(text)) if text[i] not in 'aeiouAEIOU'])

#7

Technically, list comprehensions have not been covered yet. They come up in the unit on Advanced Topics. If you really want to talk efficiency (another topic that comes up later, when we have more concepts under our belt)...


    return ''.join([c for c in text if c not in 'aeiouAEIOU']);

#8

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