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?


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
>>> 'a' == vowels
>>> "a" in vowels


so i change the statement with
if text in vowels


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:


ok cool,
now it is working.
Thanks man.


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'])


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']);


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