Codecademy Forums

Why does anti_vowel fail in some cases?

abusing the operator or :wink:

def anti_vowel(text):
result = “”
for i in text:
if i == ‘A’ or i == ‘a’ or i == ‘E’ or i == ‘e’ or i == ‘o’ or i == ‘O’ or i == ‘U’ or i == ‘u’:
result += " "
else:
result += i
return result

print anti_vowel(“Hello word”)

I wrote my code this way:

def anti_vowel(text):
  for char in text:
    if char not in "aeiouAEIOU":
      print char,

anti_vowel("dog eat dog") #prints out "d g   t   d g"

The result should be acceptable, since it does remove the vowel but the site doesn’t seem to like this solution. I wonder why? Is it because of the spaces between the printed characters?

also, and you only print the anti vowel string, you need to return it

I’m not quite sure why my code doesn’t work? It seems to remove all vowels except for the one “o” in “Hey look words!” I’m not necessarily looking for a solution, I just want to know why my code doesn’t work. Thanks for any help.

def anti_vowel(text):
lst =
final = “”
for c in text:
lst.append©
print lst
for c in lst:
if c in “aeiouAEIOU”:
lst.remove©
print lst
for c in lst:
final += c
print final
return final

removing from the same list as you are looping over, is a bad idea

lists can’t have empty spots/slots, so the moment you remove something from the list, everything to the right of the removed item shifts one space to the left

combined with the loop you are using (which moves to the next index), this causes letters to be skipped

2 Likes
vowel = ["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"]

def anti_vowel(text):
  for t, v in zip(text, vowel):
    if t == v:
      text.remove(t)
 
print anti_vowel("Happy")

Hi all, thank you very much for the sharing. While I have seen how others approached the problem, it’s still not quite clear to me where I am getting wrong, the terminal delivers ‘None’. Please can somebody help? Thank you.

1 Like

None is the absence of a return value

1 Like

I think I know where I got it wrong now, zip is not appropriate here, thank you very much

1 Like

among other things, yes

1 Like

Hi, I wrote a similar code as well and got the same output. I’m not sure I quite understand your explanation. If the loops skips the second ‘o’, wouldn’t it print 'Hy lok Wrds!" instead of ‘Hy lk Words!’? Thanks!

Consider how str.remove works. It does not remove the character at the index, but the first one it encounters from left to right. The last o is the one that will remain even though it was the earlier one that was skipped over.

1 Like

Oh I understand it now, thanks a lot!

1 Like

2 posts were split to a new topic: Intermediate spoiler - anti-vowel

def anti_vowel(text):
for char in text:
if char not in “aeiouAEIOU”:
print char,

why does this not work:(

because you need to return the anti vowel string, not just print it

Is python any more efficient at running a text.replace function versus using a for loop? I was able to get my code running with the following code:

def anti_vowel(text):
newstr = text.replace(“a”,"")
newstr = newstr.replace(“e”,"")
newstr = newstr.replace(“i”,"")
newstr = newstr.replace(“o”,"")
newstr = newstr.replace(“u”,"")
newstr = newstr.replace(“A”,"")
newstr = newstr.replace(“E”,"")
newstr = newstr.replace(“I”,"")
newstr = newstr.replace(“O”,"")
newstr = newstr.replace(“U”,"")
return newstr

If we study that code we see pattern repetition in abundance. This is a good place to consider simplifying with a re-usable block.

def anti_vowel(text):
    for target in 'aeiouAEIOU':
        text = text.replace(target, '')    # re-usable block
    return text

The new pattern says everything the old repeated pattern does, but without the repetition.

Sometimes the question is more about not inducing reader fatigue than about efficiency.