Anti_vowel


#1

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

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

I'm expecting the code to go over the entire text and replace the vowels with an empty string, which should theoretically give the correct output for whatever is put into it.

Here's my code:

def anti_vowel(text):
    vowels = "AEIOUaeiou"
    result = ""
    for char in text:
        if char in vowels:
            text.replace(char, "")
    return result

I think I'm just using the str.replace() function wrong.


#2

Where does result get its value from? Your code returns an empty string.


#3

Instead of replacing anything, add char to result if char is not in vowels =)

What your code is doing is trying to replace something in text, instead of making anything out of your String-variable "result"

vowels = "aeiouAEIOU"

def anti_vowel(text):
    
    new_string = ""
    
    for i in text:
        if not i in vowels:
            new_string += i  
    return new_string
    
print anti_vowel("abcdefg")

#4

Ah, glaring mistake there. Thank you for pointing that out.

@microresistor thank you as well, that method is much more straightforward to understand for me.

I finally got it, thanks you guys!


#5

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