Anti_Vowel


#1



Lesson 15.8: Anti_vowel


I wrote this solution for the problem, with the goal being deleting all vowels from strings.


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

The above text has been fixed and should work now.

However, the lesson is giving me the error:

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

Why would this function delete all the vowels in the test string except for the first "e"? And what can I do to fix it?


#2

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

Suppose we're testing for this..

"Hey look Words!"

at second iteration it finds vowel e there and replaces all e's with empty string.
Your string becomes "Hy look Words!"

Now at 5th iteration it sees vowel o ,Note that you're doing replace operation at unchanged value of text which is "Hey look Words!".. so now this time it mutes all o's
and your string turns out to be.. "Hey lk Wrds!"

The reason is very clear..your text string is not updated at each time after replacements.

Why would this function delete all the vowels in the test string except for the first "e"? And what can I do to fix it?

Introduce a line of code that would reflect changes done by replace() method on original string.
Maybe you can re-assign your updated new string to text ?


#3

Thank you for your help, it didn't occur to me that it wasn't updating the string! The solution was even simpler than you suggested - by just keeping the name of the string the same, it worked. Just needed to change those two words.

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

#4

Hi @al_scarface_capone,
I was just going with your code.
solution is even more simpler.(not the best but it sure eliminates lots of step from your current code)

why we've to loop through entire text and check if its in string "aeiouAEIOU"? , why not just loop over "aeiouAEIOU" ?
(also You should edit the code so no one should copy it and use it. :stuck_out_tongue:)


#5

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