Anti_vowel confusion


In the anti_vowel exercise i wrote this program below:

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

It returns Deadpl
Question is WHy????


Try not to be too scant with your questions, otherwise you may end up being thrown into Mount Doom. But I digress:

result = text.replace(i, "")

That's your main culprit.

Basically, whenever you call that line, the variable result is being replaced with text.replace(i, "") - but remember that text is always (in this case) set to "Deadpool".

In other words, each time it finds a vowel, result will look like this:
Dadpool --> Dedpool --> Deadpl

Have a go at finding another way around this exercise - for example, have your function build a separate string variable character by character, omitting the vowels.


Every time that it finds a vowel, it re-assigns result to text minus the letter. So, the first time, result is set to Dadpool, then it goes to Dedpool, and you finally end up with Deadpl.

EDIT: Whoops, looks like @nedwards already answered your question.


Thanks for your explanation
tell me something @nedwards is that you on your profile pic
because if it is i'm going to quit coding right now :smile:


It is, but about 20 years ago :grin:

I only started coding in the last few years, so I'm certainly no child genius :stuck_out_tongue_winking_eye:


Phew... :sweat_smile: