My simple but unelegant solution for anti_vowel does not work


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096#


My code does not seem to get rid of the vowels... I keep getting the following error message:

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


I was hoping this would do the trick, but I sense I am using a wrong building block, i.e. I probably can't use the .strip command in this context!? Thanks for the help! :slight_smile:


def anti_vowel(text):
    for x in text:
        if x == "a":
            text.strip(x)
        elif x == "e":
            text.strip(x)
        elif x == "i":
            text.strip(x)
        elif x == "o":
            text.strip(x)
        elif x == "u":
            text.strip(x)
    return text


#2

looking at the docs - strip, strip removes leading and trailing characters, for example:

print " hello world ".strip()

see how the leading and trailing spaces are removed? strip() doesn't seem to be the right function for anti_vowel


#3

Thanks! Is there another function that I am not aware of yet that would make this work if substituted for .strip()?


#4

You are overthinking this. Try following this template:

def FUNCTION(PARAMETER):
    for INDIVIDUAL_ITEM in PARAMETER:
        if INDIVIDUAL_ITEM in "aeiouAEIOU":
            PARAMETER = PARAMETER.replace(INDIVIDUAL_ITEM, "")
    return PARAMETER

Make sure not to just follow the template and move on. I want to make sure that you understand how and why this works. If you have any questions, just ask.


#5

personally, i would just make a string/list to which we append the constinants. using not in like @aquaphoenix17 suggested is better then the many if condition you would need otherwise


#7

Where did I use not in?

I mean, I guess you could use not in but the code would be longer...

def FUNCTION(PARAMETER):
    VARIABLE = ""
    for INDIVIDUAL_ITEM in PARAMETER:
        if INDIVIDUAL_ITEM not in "aeiouAEIOU":
            VARIABLE = VARIABLE.append(INDIVIDUAL_ITEM)
    return VARIABLE

Personally, I prefer my way better, but to each their own, I guess.


#8

this approach is really bad? .append() is a method for lists, not strings. append will update the list and return None, then storing result in variable will mean VARIABLE contains None


#9

Then I guess you can't use not in... or at least not the way I did.


#10

VARIABLE = [] and then use .join() to get to string after list

or use VARIABLE += INDIVIDUAL_ITEM` if you want to keep VARIABLE a string


#11


def FUNCTION(PARAMETER):
    VARIABLE = ""
    for INDIVIDUAL_ITEM in PARAMETER:
        if INDIVIDUAL_ITEM not in "aeiouAEIOU":
            VARIABLE += INDIVIDUAL_ITEM
    return VARIABLE

#12

cooool, thanks a lot for this!

one question if I may - just to confirm:

if INDIVIDUAL_ITEM in "aeiouAEIOU":

does this line just check whether the character (INDIVIDUAL_ITEM) is found within the string "aeiouAEIOU", and if so it will replace it with "", i.e. nothing within my PARAMETER, and thus I am left with only consonants?

Thanks a bunch again!


#13

yes it does

yes exactly


#14

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