8. anti_vowel


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
def anti_vowel(text):
text=raw_input("Type ur text here: ")
if “text contains vowels.”:
return text
return text

it return this error:
Oops, try again. Your function fails on anti_vowel(“Hey look Words!”). It returns “” when it should return “Hy lk Wrds!”.

<What do you expect to happen instead?>


Replace this line with your code.

<do not remove the three backticks above>

Hey @webpro43670 ! The quotation marks around your if statement is probably tripping your code up as you don’t need them. Having them makes the words a string, float, integer, etc. Remove them and try again. Or try this code:

def anti_vowel(text):
    word = ''
    for letter in text:
        if not (letter in 'aeiouAEIOU'):
            word += letter
    return word

Hope that help! :slight_smile:


thanks man, that really did it.

@chipjumper36731, I really like your solution! It is succinct and has only one loop.

Here is what I came up with, using a nested for loop:

def anti_vowel(text):
    vowels = "aeiouAEIOU"
    for i in text:
        for j in vowels:
            if i == j:
                text = text.replace(i, "")
    return text

@webpro43670 Glad I could help! :upside_down: and for the record, I’m a woman :joy:

@wowpalvabbit Thanks for the compliment! And your nested for loop is really clever, I like the way you designed it, and it may come in handy during the censor exercise, as they want you to do a similar thing :slight_smile:

1 Like

sorry, my bad…didn’t know.

You’re fine! Nothing wrong with that. Glad to help!:slight_smile:

I’m using something similar but w/ a lower() to convert the string to lowercase. However this is not working…

word = raw_input("Type a word: ")
lowerC = word.lower()
def anti_vowel(text):
    vowel = "aeiou"
    for i in vowel:
        text = lowerC.replace(i,"")
    return text

print anti_vowel(text)

text is a variable, it is not defined

But after that fix replace() still not working… Thanks though!

[quote=“datapro19957, post:10, topic:48624”]
word = raw_input(“Type a word: “)
lowerC = word.lower()
def anti_vowel(text):
vowel = “aeiou”
for i in vowel:
text = lowerC.replace(i,””)
return text

print anti_vowel(text)

cant seem to find wats wrong with it but why dont u try the code as stated above by chipjumper36731

def anti_vowel(text):
vowel = raw_input("Type a word: ")
for i in text:
lists = ‘aeiouAEIOU’
if i not in lists:
vowel += i
return vowel

What is wrong in this?

vowel = [‘a’,‘e’,‘i’,‘o’,‘u’,‘A’,‘E’,‘I’,‘O’,‘U’]
string = raw_input("Input text: ")
def anti_vowel(string):
new = “”
for i in range(len(string)):
for j in range(len(vowel)):
if string[i] != vowel[j]:
new += string[i]
return new

You are using string[i] to refer to a character within a string.

could you please correct my code

@datapro19957 your problem is that you turn the string into lowercase outside the actual function. Since it is not part of the function, Python is probably not running the command making the text not lowercase. Try putting the second line of code inside the anti_vowel function. If it still doesn’t work then let me know.

Good luck! :slight_smile:


def anti_vowel(text):
…count = len(text)
…x = 0
…ret = ‘’
…while x < count:
…if not (text == “e” or text == “o” or text == “a” or text == “i” or text == “u” or text == “A” or text == “E” or text == “I” or text == “O” or text == “U”):
…print text
…ret += text
…x += 1
…return ret

Hi! Can you help me with figuring out why isn’t my code working? As for me, it should be doing the same thing you wrote down. Yet, it returns ‘none’. Thank you in advance!

def anti_vowel(text):
vowellist = ‘euioaEUIOA’
new = ‘’
for letter in text:
if letter in vowellist == False:
new += letter

You have to add a pair of parentheses to the condition of the if and now, with proper indentations, your code should work:

def anti_vowel(text):
  vowellist = 'euioaEUIOA'
  new = ''
  for letter in text:
    if (letter in vowellist) == False:
      new += letter

For anyone interested in more Pythonic solutions here is mine, more compact, solution using filter function:

def anti_vowel(text):
    return filter(lambda c: c.lower() not in ['a','e','i','o','u'], text)