8/15 anti_vowel


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/antivowel?action=lesson_resume

It seems like the last “o” in “Words” is skipped. This is the error message.
Your code looks a bit off--it threw a "'str' object has no attribute 'copy'" error. Check the Hint if you need help!

I would expect it to run smoothly since it already removed the "o"s in look. Why is the “o” in “Words” skipped?

def anti_vowel(text):
  l=[]
  s=""
  for char in text:
    l.append(char)
  for c in l:
    if c in 'aeiouAEIOU':
      l.remove(c)
  for n in l:
    s = s + n
  return s


`


#2

Well, I’m not expert, but I think you can make it simple:


def anti_vowel(text):
  lista = []
  for v in text:
    if v not in "aeiouAEIOU" :
      lista.append(v)
  return "".join(lista)

#3

Yeah I am going to make it simpler later but I’m quite confused why did the “o” in “Words” not removed.


#4

lets say we have the following string we want to remove the vowels of:

anti_vowel("look")

so look goes from index 0 till 3 (inclusive):

l   o   o   k
0   1   2   3

the problem is that strings and lists can’t have empty indexes.

so the moment the o at index 1 is removed, everything to the right of the removed letter shifts one index to the left so we get:

l   o   k
0   1   2

good, removal completed, no empty indexes left, so the loop moves from index 1 to index 2. This causes successive vowels to be skipped (given the o moved to from index 2 to index 1)

in your case, the o in look gets removed because remove() will remove the first instance it can find, so the moment the o in word is found, the o in lok gets removed

this is why remove is not a recommended approach for this program


#5

I see. If I were to continue with this approach, is there a way for me to overcome this problem?


#6

despite my recommendation that remove is not a good approach?

Well, obviously this problem only occurs when you remove from the same list as you are looping, but given you have both a list and a string, you should be able to overcome the problem


#7

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