Anti_Vowel Question


#1



anti_vowel


Okay...I have never posted on here before, but I need some help. I was trying to do this exercise without coming to the Q&A, to just do it with what I could remember off the top of my head. With that said, I see now that I went about this a more difficult and obviously wrong way. I've seen what I did wrong, but I still don't understand why what I have won't work and was hoping that someone could take a look and explain what is happening because it's not doing what I thought it would do apparently. I'm getting the following error:

Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hy lk Words!" when it should return "Hy lk Wrds!".


I am thinking that my code is running each character in (text) through the loop individually and removing it if it matches but it stops after removing three vowels and it leaves the fourth. It has already removed two 'o' and I'm stuck as to why it leaves the last one.

Even though this isn't the correct answer to the practice, I'd still like to understand why it isn't working and know what I did wrong...


def anti_vowel(i):
	i = list(i)
	for ltr in i:
		if ltr == "a" or ltr == "A":
			i.remove(ltr)
		elif ltr == "e" or ltr == "E":
			i.remove(ltr)
		elif ltr == "i" or ltr == "I":
			i.remove(ltr)
		elif ltr == "o" or ltr == "O":
			i.remove(ltr)
		elif ltr == "u" or ltr == "U":
			i.remove(ltr)
	text="".join(i)
	return text


#2

When you loop over you i (which is list now) and you find any vowel,You delete it.
Your list i length gets decreased and all the letters shift by 1 (index number is changed now).
so It leaves some vowels there.

You can create another list(copy of i)
and loop over i but remove from that copy and convert it to string and return it!

How to make a copy of list?

copy_list = i[:]

(Also your could have shortened entire code inside for loop ,and overall code)


#3

That's exactly what I needed. I didn't take that into consideration and i was going nuts truing to see the problem. I appreciate you taking a look, thanks..


#4

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