Why dosn't this code work?


#1


def anti_vowel(text):
    L=list(text)
    T=['a','e','u','i','o','A','E','U','I','O']
    for i in range(0,len(T)):
        x=T[i]
        for l in L:
            if l==x:
                L.remove(l)
    return "".join(L)

Continuing the discussion from 8. Anti-Vowel:


#2

Because remove a particular bad idea for this problem, why? the moment you remove a item, the index is going to shift, really simple example:

t   e   a   r
0   1   2   3

i put spaces in so you can see it better. Now, the moment the e at index one gets removed, everything shifts, so the a will end up at index one, but the loop will just going to the next position (the counter of the loop increases), so the a gets skipped.

Now, it can be done with remove, but you need a while loop, which is causing index problems, so you need to catch that. Bad idea, i recommend to append consonants to a new list, and then join that


#3

There are few mistakes in your code first you haven't lopped through your text and by stating range you are compairing numbers.
To make it work first
Make a empty string like words = ""
then you have to loop through text like this
for i in text:
then compair that i to the string T
if i not in T:
append the i to the words
words.append(i)
then joint them with empty space
"".join(words)
return words


#4

Got it, thank you a lot


#5

@s09abcd, i agree there are mistakes. But i disagree with this:

He is looping through the vowels here:

for i in range(0,len(T)):

then converting the number into letter (vowel):

x=T[i]

then he loops through the text:

for l in L:

i will give you this is not the most efficient, looping for each vowel over the entire text. But it is happening


#6

opps my bad that was kind of long way to loop through but make scence.


#7

I agree with you on that, absolutely :slightly_smiling: Actually, his method could have worked, but then you have to keep looping over the vowels until no more vowels are there, but that is such a resource waste. But good to hear we agree :slightly_smiling:


#8

Sorry, but my looping inside L=list(text) was through items itself instead of indexs, so removing dose not affect the process of looping . I just want to mention that I tried this solution in the "Interactive Editor for Python" and it dose work !
So I'm a little bit confused


#9

I'm sure we both did something like that when we first started programming :slightly_smiling: at least i did :smile:


#10

@boudalh, did you run all the cases the exercise did? The exercise runs many different sentences to validate your code is absolutely correct

@s09abcd, yes, agree. I hope you saw my remark as positive feedback to help you :slightly_smiling: You can learn from helping other people


#11

Yes, of course it is helpful thanks