Anti-Vowel: Why is it wrong?


#1

What's wrong with my code?

def anti_vowel(text):
    to_list = list(text)
    vowel = ["a","e","i","o","u","A","E","I","O","U"]
    for i in range(len(to_list), -1, -1):
        if to_list[i] in vowel:
            to_list = to_list - to_list[i]
    return ("").join(to_list)

print anti_vowel("Hey look Words!")

Traceback (most recent call last):
File "python", line 9, in
File "python", line 5, in anti_vowel
IndexError: list index out of range


#4

here:

for i in range(len(to_list), -1, -1):

strings are zero indexed based (first index is zero) while len() start counting at one. so you will need to subtract one from length, otherwise you get an index error

to remove based on index, use del


#5

@aquaphoenix17 with two little fixes, the problem is solved. Why recommend a completely different approach?


#6

Hence why I deleted the post.


#7

I am sorry but i don't understand.

Is it like this?:

For i in range(len(to_list) - 1, -1, -1):

However, i got an error for this.

How to use the del to subtract one from length?


#8

keywords like for are case sensitive, for should be all lowercase

please post your full code


#9

What I mean is how you use the "del" to solve my error(subtract one from the list length)?


#10

for how to use built-in function documentation was invented


#11

I don't understand, please clarifty


#12

Please see the Python documentation for how to use del. The documentation for Python 2.7 can be found here.


#13

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