Anti_vowel - problem with while loop


#1



def anti_vowel(text):
	i = 0 
	while i < len(text):
		if text[i] in "aeiouAEIOU":
			text[i] == ""
		i += 1
	print text

I can not find the bugs in these line of codes. Please help.


#2

Whenever you want to get a good look at what you program is doing, start adding print statements.

Head over to this site so you can experiment without getting "Oops" errors. https://repl.it/languages/python

First, let's make sure that, as we loop through the text string, that you get what you are expecting.

def anti_vowel(text):
    i = 0 
    while i < len(text):
        if text[i] in "aeiouAEIOU":
            # let's see what text[i] is
            print "text[i]: ", text[i]
            text[i] == ""
            i += 1
    print text

# call your function	
anti_vowel("The quick brown fox jumps over the lazy dog.")

So far so good?

Now, after you set text[i] == "", print it again to see what value text[i] is now. Add this line after you attempt to change the value of text[i]:

print "text[i](after): ", text[i]

Whoa, was that unexpected -- what is going on?

What you have just discovered is that python strings are immutable. You can Google that if you would like to learn more.

You now know that you need to think up a new way to go at this problem.


#3

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