Anti vowel code not working


#1

Practise makes perfect https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096
I have to print the string as it is except for the vowels. I was able to use a for loop to do it but am unable to do so with the while loop. I cannot seem to debug my fault.

My code

def anti_vowel(text): 
    s = ""
    le = len( text ) - 1 #since index starts at 0 we must negate by 1
    while le >= 0: 
        if text[le].lower() not in "aeiou":
            s += text[le]
        le -= -1
    return s

I get the string out of index error, even though I have (apparently) taken care of it.Help please!


#2

this line:

le -= -1

lets get rid of the shorthand, lets write it fully:

le = le - -1

oh dear, --1 is +1, aka:

le = le + 1

that is going to cause a index error. Another issue, since len is the highest number (last letter), you append this to a empty string, then the new latest letter you append after it, and so on, in essential reversing the string, good thing, but not for this exercise.

I would actually the reverse:

counter = 0
le = len(text)
while counter < le

and then increase the counter, of course, you are free to create your own solution, and i would encourage you to :slight_smile:


#4

Change line 6: s += text[le] to s = text[le] + s

Change line 7: le -= -1 to le -= 1


#5

Thanks ! I'll be more careful using shorthand now!
Btw I dont know what was I thinking while writing that code, the loop reverses the string xD!! I guess I was feeling too lazy of writing the loops again and again that I copied the while from my previous (reverse) code and blindly pasted it ! :sweat_smile: