Anti_vowel help please!


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096


Hi all, can I get your advice please (seems like many people have problems with this, and my method is quite different).

I receive an error "string index out of range" error when I run it

What I'm trying to do is to loop through the letters of vowels and checking it against the string in the function index[0],[1]... etc. and if it's a vowel, to pass, move on to the next index position.

I tried to set the bound in the 1st if statement but still the error persists.

Is what I pictured in my head correct and is this method feasible? thanks!

def anti_vowel(example):
    answer = ""
    vowels = 'aeiouAEIOU'
    pos = 0
    for vow in vowels:
        if vow == example[pos] or pos >= len(example) - 1:
            pass
        else:
            answer += example[pos]
        pos += 1
    return answer
    
anti_vowel('abcdefghi')


#2

if you want to do this you will need two loops, the first loop to loop over example and the second to loop over the vowels to see if current character in example is a vowel


#3

Ohh. but with nested loops, do I need to add a Continue or any other command to stop the loop for the 1st character has been checked? (ie to stop the loop from checking the 1st character again with the 2nd vowel

Been experimenting with break as well

def anti_vowel(example):
    answer = ""
    vowels = 'aeiouAEIOU'
    for char in example:
        for vow in vowels:
            if char == vow:
                continue
            else:
                answer += char
            return answer

print anti_vowel('abcde')

#4

no no, the we only should char to answer after the second loop has finished running (this determines this char is not a vowel)

if char equals vowel, we should break the loop.

then we can use for/else to determine if char needs to be added to answer

if the loop breaks, else of for/else will not run, this only happens when loop condition is becomes false (char is not a vowel)


#5

Ah got it. Took me a while to visualize it, but i think I got it in the end!
Just wondering, is there an more efficient way of doing this?
I read that nested loops are discouraged. (though I think that as long as we get the job done there's no need to care about what goes on behind the scene)

def anti_vowel(example):
    answer = ""
    vowels = 'aeiouAEIOU'
    for char in example:
        for vow in vowels:
            if char == vow:
                break
        else:
            answer += char
    return answer

print anti_vowel('Hey Look')

#6

you could use in instead of the second for loop (which check if something is present in list or string, but this very likely also uses a loop under the hood

in python, strings are immutable. So concatenating a string like you do here:

answer += char

will create a new string with the combined result, using lists would be more efficient.


#7

do you mean like this?

def anti_vowel(example):
    answer = ''
    vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    for char in example:
        if char not in vowels:
            answer += char
    return answer

print anti_vowel('Hey Look')

#8

vowels can be a string, i meant answer should be a list, that is more efficient.


#9

oh. I tried that! but this particular exercise required the output to be in string format, though it'll be quite simple to convert answer to a string from a list anw.

Thanks for your help mate!


#10