Anti_vowel


#1





Oops, try again. Your function crashed on Hey look Words! as input because your function throws a "string index out of range" error.


I don't know how to fix this bug. Can anyone point out my mistakes?


def anti_vowel(text):
    vowel = "aeiouAEIOU"
    result = ""
    x = 0
    while x <= len(text):
        for c in text:
            if c != vowel[x]:
                result += c
                x += 1
    return result


#2

strings are zero index based, so for example:

text = "hello"

the length will be 5, and the indexes: 0, 1,2, 3, 4.

so you want lesser then (<) len(text), not lesser or equal to (<=) of length text


#3

you are right, but I still get the same error message


#4

you use the while loop to loop over text, so if text is longer then vowel, this will cause a problem:

if c != vowel[x]:

if you are planning to use x for your vowels, why do you loop over text and not vowel?


#5

You're told you have an invalid index, so you might start by figuring out which value you used for an index and for what sequence.

Either could be wrong but it's probably the index that's wrong, and if so, then you'd look at how you got it (and compare to how you think it should have come up with all the correct indexes)

It's also worth considering if the indexes could have been generated in a less error-prone way that you could just read and say "that's obviously correct" and not worry about it


#6

I still don't see what's wrong now. Everything seems right to me so I must have missed something or misunderstood some basic principle. I still get the same error.

def anti_vowel(text):
    vowel = "aeiouAEIOU"
    result = ""
    x = 0
    while x < len(vowel):
        for c in text:        
            if c != vowel[x]:
                result += c
            x += 1
    return result

By the way, I only started programming and learning Python about a week ago and I'm still not sure how to use while- and for-loops. I think the problem may be in my misunderstanding of the for-loop


#7

add a function call so you can see what is happening.

Also, you increase x within the for loop, this still means x can become greater then the length of vowel


#8

Loops are fairly easy to wrap your head around.

While-loops continue as long as the condition evaluates to True
for-loops iterate over each value of the iterable (like a list)

while the jar isn't empty, take a cookie and eat it
for each cookie in the jar, eat it


#9

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