Dont see any reason why this dosent work


#1



it wont remove the o for some reason, it did for the others so why isn't it working on the o in word?


def anti_vowel(text):
    text = list(text)
    vowels = ["a", "e", "i", "o", "u"]
    uppr_vowels = [i.upper() for i in vowels]
    for r in text:
        for y in vowels:
            if r == y:
                text.remove(r)
        for w in uppr_vowels:
            if r == w:
                text.remove(r)
    text = "".join(text)
    print text
    return text


anti_vowel("Hey look Words!")


#2

remove is a dreadful method to solve the problem. the problem is in look, lets say look is the string we we want to remove vowels of:

l   o   o   k
0   1   2   3

the moment the o of index 1 gets removed, we have a problem. strings can't have an empty index, so the o of index 2 shifts to index 1, while the for loop continues to index 2 which now contains k

why then seems that the problem is in words? because remove removes the first instance it can find, so when it encounters the o at words, the o from lok gets removed

i recommend a different approach


#3

huh, that's weird. when I put in code to check what it is doing (putting prints in the loops) it does go over o but doesn't get rid of it. also why isn't it skipping the second o in look then? according to what you say it should, but it goes over the o in words not the one in look.


#4

i explained this?

due to index shift the second o in look gets skipped, but then it encounters the o in words, but remove() will remove the first o which it can find, which is in lok

from the documentation:

list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.

this is for list, but the same applies for strings


#5

oh.
should I just make it do the whole process a second time then? would that catch anything that was leftover?


#6

no, it won't work. The exercise has some cases with successive vowels to test your code, you can't make it work

I would reverse the approach, append constants to a new list/string, so much easier and cleaner


#7

fixed it, just made it do the whole process 10 times


#8

wow, so much elegance.

i am sure you can do better :slight_smile:


#9

hay, if it ain't broke don't fix it.


#10

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