8.anti_vowel


#1

The way i tried to solve this was to create a variable ‘vowel’ and use a For loop to check if each alphabet is one of the letters in ‘vowel’. Here’s the code

def anti_vowel(text):
** vowel = “aeiou”**
** for letter in text:**
** if letter.lower() in vowel:**
** new_text = text.replace(letter,"")**
** return new_text**

The error message i got was this: Your function fails on anti_vowel(“Hey look Words!”). It returns “Hey lk Wrds!” when it should return “Hy lk Wrds!”.

The loop failed on removing the ‘e’. Can anyone help me with this?


#2

Hi @erbazkamran,

Precisely what does this line do during each iteration of the loop? …

  new_text = text.replace(letter,"")

Consider whether or not the value of text is changing and what consequences that has for the value of new_text.


#3

it replaces letter with “”. Meaning the letter becomes absent from the position


#4

In what variable does it become absent from the position?

You may want to use some temporary print statements to check the values of variables during each iteration of the loop.


#5

def anti_vowel(text):
** vowel = “aeiou”**
** for letter in text:**
** if letter.lower() in vowel:**
** text = text.replace(letter,"")**
** return text**

This worked although it’s not clear why.


#6

Now you are revising the value of text during each iteration, which is what you were not doing originally.

This did not change the value of text

  new_text = text.replace(letter,"")

Consequently, during each iteration, you lost the work that you had done during the previous iteration.


#7

I understand. Thankyou :slight_smile:


#8