Anti vowel code not working


Practise makes perfect
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!


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:


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

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


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: