Anti_vowel - "string index out of range" error


#1

My code is running fine, but at the bottom of the screen it says "Oops, try again. Your code looks a bit off--it threw a "string index out of range" error. Check the Hint if you need help!"

In the code window, however, there are no errors and everything appears to be working exactly as I expected.

This is in Exercise 8 in Practice Makes Perfect (Anti_Vowel)

Here is my code:

string=raw_input("Enter a string of text to remove the vowels (y is not a vowel): ")

new_string=[]
chars=len(string)

def anti_vowel(string):
    text=string.upper()
    print text
    for c in range(chars):
        if text[c] != "A" and text[c] != "E" and text[c] != "I" and \
        text[c] != "O" and text[c] != "U":
            new_string.append(string[c])
            print new_string
    return ''.join(new_string)
            
print anti_vowel(string)

I have a few extra prints in there that allows me to see what is happening and when it is happening, so here is my print out - which appears to be working exactly correctly:

Enter a string of text to remove the vowels (y is not a vowel): Why won't this work?
WHY WON'T THIS WORK?
[u'W']
[u'W', u'h']
[u'W', u'h', u'y']
[u'W', u'h', u'y', u' ']
[u'W', u'h', u'y', u' ', u'w']
[u'W', u'h', u'y', u' ', u'w', u'n']
[u'W', u'h', u'y', u' ', u'w', u'n', u"'"]
[u'W', u'h', u'y', u' ', u'w', u'n', u"'", u't']
[u'W', u'h', u'y', u' ', u'w', u'n', u"'", u't', u' ']
[u'W', u'h', u'y', u' ', u'w', u'n', u"'", u't', u' ', u't']
[u'W', u'h', u'y', u' ', u'w', u'n', u"'", u't', u' ', u't', u'h']
[u'W', u'h', u'y', u' ', u'w', u'n', u"'", u't', u' ', u't', u'h', u's']
[u'W', u'h', u'y', u' ', u'w', u'n', u"'", u't', u' ', u't', u'h', u's', u' ']
[u'W', u'h', u'y', u' ', u'w', u'n', u"'", u't', u' ', u't', u'h', u's', u' ', u'w']
[u'W', u'h', u'y', u' ', u'w', u'n', u"'", u't', u' ', u't', u'h', u's', u' ', u'w', u'r']
[u'W', u'h', u'y', u' ', u'w', u'n', u"'", u't', u' ', u't', u'h', u's', u' ', u'w', u'r', u'k']
[u'W', u'h', u'y', u' ', u'w', u'n', u"'", u't', u' ', u't', u'h', u's', u' ', u'w', u'r', u'k', u'?']
Why wn't ths wrk?
None

Can anyone help me figure out where the index problem is occurring? It is very hard to figure out what could be going wrong - when the code itself appears to be working correctly?


#2

you are dealing with a scoping issue here . Best if you move these lines into the function


#3

I am not clear what you mean by a scoping issue but yes, just moving those into the function solved the problem. Keeping them inside the function makes sense - I'm not sure why I put them outside! Thank you for the help!


#4

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