15.8 anti_vowel - Why won't my code work?


#1

def anti_vowel(text):
text=list(text)
a=text.copy()
for i in a:
    if i in 'aeiouAEIOU':
        text.remove(i)
emp=''
for i in text:
    emp = emp + str(i)
print(emp)

anti_vowel("aeYY")

This is my code. I know a workaround and I know how to make it work. I just wanted to know why this does not work!


#2

The error message is pointing to this line.The copy() method does not exist for lists.


#3

But this perfectly works on IDLE. And help(list) does show a list.copy() in IDLE.


#4

Firstly there are different versions of python, second not all implementations have the same build in methods.

If you want cross platform you need to have the packages and then export them in your distribution. The course can be any of the 2.7 series of python, also some of the builtins are blocked from use.

Personally I would just use the regex module for this.

from re import sub
def anti_vowel(input_):
    return sub(r"[aeiouAEIOU]", '', input_)

The above takes an argument, then it takes what it will replace it with, and finally the string to work on.

The letters in the bracket means that for each item of any of them match the bracketed items it will sub that position.


#5

I did not know that! Thanks a lot! :smile: