Can't get vowel out of words 8. anti_vowel


#1

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

The other vowels are removed, but the “o” in “Words!” is still there. The error message is “Your function fails on anti_vowel(“Hey look Words!”). It returns “None” when it should return “Hy lk Wrds!”.”

I want the result whether return or print to be “Hy lk Wrds!”


def anti_vowel(text):
  letters = []
  vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
  for letter in text:
    letters.append(letter)
  for character in letters:
    if character in vowels:
      letters.remove(character)
  print letters
  print "".join(letters)



#2

your function needs to return the anti vowel string, currently it only prints the anti vowel string

remove() is a difficult approach to solve this problem, given you have to deal with index shift.

the problem is that the moment you have successive vowels (like in look) the first o gets removed, while the second get skipped

lets say our list is look:

l   o   o   k
0   1   2   3

the list + there indexes, the moment you remove the o at index 1, everything to the right shifts to the left so we get:

l   o   k
0   1   2

given lists can’t have empty indexes, then the loop progresses to the next index (2), causing the second o to be skipped

this problem only occurs when you are looping over the same list as you are removing from, so you could loop over text while removing from letters, then this problem doesn’t occur

or you could go for the the opposite approach, append consonants to a new list


#3

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