+ 1

Please, explain how x is a local variable and where my defined function is wrong??

def anti_vowel(text):   textnew=list(text)   for x in textnew and x in ["a","e","i","o","u","A","E","I","O","U"]:     textnew.remove(x)   return("".join(text)) what is wrong in my above python code to define a function with its vowels removed please explain??

4th Nov 2017, 4:34 AM
Abhinav
Abhinav - avatar
4 Respostas
+ 14
Here's something I tried. I used a while to remove every instance of every vowel from textnew. What happens with your code is that .remove() only removes the first instance of an item, and one of the many o's doesn't get removed. def anti_vowel(text): textnew=list(text) vowels = ["a","e","i","o","u","A","E","I","O","U"] for x in vowels: while (x in textnew): textnew.remove(x) return("".join(textnew)) print(anti_vowel("Hey You!"))
4th Nov 2017, 5:53 AM
Tamra
Tamra - avatar
+ 9
x is a local variable that only lasts within the for. You're defining x twice, once to count for textnew, and once to count the vowel list. You can only define a variable once. Because of how you're using the for (there's nothing in textnew you're counting), all you need is: for x in ["a", "e", ..., "U"] in order to remove the vowels from the textnew list. Also, you should be returning textnew, not the original text.
4th Nov 2017, 4:49 AM
Tamra
Tamra - avatar
0
thanks, can you tell me what's wrong with my code now def anti_vowel(text):   textnew=list(text)   for x in textnew:     if(x in ["a","e","i","o","u","A","E","I","O","U"]):       textnew.remove(x)   return("".join(textnew)) print(anti_vowel("Hey You!"))
4th Nov 2017, 5:39 AM
Abhinav
Abhinav - avatar
0
it returns by 1k words! when it should had returned by 1k wrds! for input hey look words!
4th Nov 2017, 5:41 AM
Abhinav
Abhinav - avatar