8. anti_vowel

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096#

<In what way does your code behave incorrectly? Include ALL error messages.>
def anti_vowel(text):
vowels=‘a,e,i,o,A,E,I,O,U’
text=raw_input("Type ur text here: ")
if “text contains vowels.”:
del(vowels)
return text
else:
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?>

```python

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:

11 Likes

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
5 Likes

@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)
[/qu

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]
break
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:

@chipjumper36731

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
return(new)

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
  return(new)

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)
2 Likes