Anti_vowel confusion


#1

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
    
     print(anti_vowel("Deadpool"))

It returns Deadpl
Question is WHy????


#2

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.


#3

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.


#4

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:


#5

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:


#6

Phew... :sweat_smile: