Anti-vowel not entirely anti-vowel


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096#


This is the error that I'm getting:
Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hy lk Words!" when it should return "Hy lk Wrds!".


I'm not sure why it is not removing the final "o".


def anti_vowel(text):
    new = list(text)
    for c in new:
        if c in "aeiouAEIOU":
            new.remove(c)
    return ''.join(new)


#2

@lconant Problem exists in this part of code..

for c in new:
        if c in "aeiouAEIOU":
            new.remove(c)

When you start looping at new list and then check if it (vowel) is present in vowel string.
Suppose you find one ,then you just remove it.
In next loop,Your list is not same as last one as you removed one element of the list.(It's length have changed as you removed one item)
Thus it skips few items of list and you get "o" left (When CC checks at anti_vowel("Hey look Words!")).

Instead deleting from new..create separate copy of new list..then delete items from it and return (by using join() method on it to convert back it to string)

One more thing,the list you create should not have same reference as yournew list.
use this syntax to create this list..

new_list = new[:]
usenew_list as loops main list instead of new.

do reply if it solved the problem or not!


#3

Yes! That solved it. Thank you!


#4

I used a slightly different approach. I started with a new empty string newtext then pushed the letter to it if it was not in text. Looping through text with a for i in range command:

if text[i] not in "aAeEiIoOuU":
newtext=newtext+text[i]


#5

I got another solution! Use not in to get all other characters out of the list, append all these to another list and return that.

def anti_vowel(text):
txt = list(text)
new = []
for i in txt:
if i not in "aeiouAEIOU":
new.append(i)
return "".join(new)


#6

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